473,577 Members | 3,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

2nd try Sorting Algorithm.

AMP
Hello,
I posted this morning and recieved no responces., so I decided to
simplify my question.
How do I stop an exception from being thrown after the first run
through of the while clause:

private void InsertionSort_F unction(uint[] IntCard,int length)
{
for (int j = 1; j <= (length-1); j++)
{
uint CurrentCard=Int Card[j];
int i =j-1;

{
while ((IntCard[i] CurrentCard) && (i 1))
{
IntCard[i + 1] = IntCard[i];
i = i - 1;

}
}
IntCard[i+1]=CurrentCard;
}
}
}

Thanks
Mike

Nov 29 '06 #1
3 1813
On 29 Nov 2006 11:00:26 -0800, "AMP" <am******@gmail .comwrote:
>Hello,
I posted this morning and recieved no responces., so I decided to
simplify my question.
How do I stop an exception from being thrown after the first run
through of the while clause:

private void InsertionSort_F unction(uint[] IntCard,int length)
{
for (int j = 1; j <= (length-1); j++)
{
uint CurrentCard=Int Card[j];
int i =j-1;

{
while ((IntCard[i] CurrentCard) && (i 1))
{
IntCard[i + 1] = IntCard[i];
i = i - 1;

}
}
IntCard[i+1]=CurrentCard;
}
}
}

Thanks
Mike
I would have a look at the order in which you have written your while
statement: while ((IntCard[i] CurrentCard) && (i 1))

This will be evaluated left to right, so the first thing it tries is
to access IntCard[i]. If i is an invalid value, this will throw an
exception. By reordering the statement as: while ((i 1) &&
(IntCard[i] CurrentCard)), the test on i will come first which
should prevent IntCard[i] ever being accessed with an invalid i. When
you fix this, you should find that you have another problem which the
first problem was masking.

Other comments on your code:

1 C# arrays come with their length built in, so there is no need for a
separate parameter to hold it. With a separate parameter there is
scope for errors, just use IntCard.Length instead.

2 You have an anonymous pair of braces around the while statement.
They do not perform any useful function.

3 As a matter of style, and better maintainability , prefer '<' rather
than '<=' for the control variable test in a for loop:

for (int j = 1; j < length; j++)

Better maintainability because other C# (and C and C++ and Java)
programmers will expect for loops to look like that unless there is a
specific reason not to.

4 You are declaring CurrentCard and i inside a for loop. Probably
better to declare them once just outside the loop rather than each
time inside the loop.

5 Microsoft style guidelines say that you should use an initial lower
case (camelCase) for variables, hence prefer 'currentCard' to
'CurrentCard' etc. An initial capital (PascalCase) is used for
classes, functions and others, so your InsertionSort_F unction is OK
except for the underscore which Microsoft standards discourage.
InsertionSortFu nction would conform to the standard.

rossum

Nov 29 '06 #2
Hi,

AMP wrote:
Hello,
I posted this morning and recieved no responces., so I decided to
simplify my question.
How do I stop an exception from being thrown after the first run
through of the while clause:
It would be helpful to know which exception, but I think that it's
because you're modifying an array in a loop iterating the same array.

The solution is to create a temporary array with the same length and
type, copy the cards to that temporary array without modifying the
original one, and then copying the temp array's reference to the
original one.

That said, what does your method do? The IntCard array is not a "ref"
parameter, so the one outside of the method will not be modified.

HTH,
Laurent
>
private void InsertionSort_F unction(uint[] IntCard,int length)
{
for (int j = 1; j <= (length-1); j++)
{
uint CurrentCard=Int Card[j];
int i =j-1;

{
while ((IntCard[i] CurrentCard) && (i 1))
{
IntCard[i + 1] = IntCard[i];
i = i - 1;

}
}
IntCard[i+1]=CurrentCard;
}
}
}

Thanks
Mike

--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Nov 29 '06 #3
AMP
rossum,
Thanks,(And I did find the other problem)
Mike
rossum wrote:
On 29 Nov 2006 11:00:26 -0800, "AMP" <am******@gmail .comwrote:
Hello,
I posted this morning and recieved no responces., so I decided to
simplify my question.
How do I stop an exception from being thrown after the first run
through of the while clause:

private void InsertionSort_F unction(uint[] IntCard,int length)
{
for (int j = 1; j <= (length-1); j++)
{
uint CurrentCard=Int Card[j];
int i =j-1;

{
while ((IntCard[i] CurrentCard) && (i 1))
{
IntCard[i + 1] = IntCard[i];
i = i - 1;

}
}
IntCard[i+1]=CurrentCard;
}
}
}

Thanks
Mike
I would have a look at the order in which you have written your while
statement: while ((IntCard[i] CurrentCard) && (i 1))

This will be evaluated left to right, so the first thing it tries is
to access IntCard[i]. If i is an invalid value, this will throw an
exception. By reordering the statement as: while ((i 1) &&
(IntCard[i] CurrentCard)), the test on i will come first which
should prevent IntCard[i] ever being accessed with an invalid i. When
you fix this, you should find that you have another problem which the
first problem was masking.

Other comments on your code:

1 C# arrays come with their length built in, so there is no need for a
separate parameter to hold it. With a separate parameter there is
scope for errors, just use IntCard.Length instead.

2 You have an anonymous pair of braces around the while statement.
They do not perform any useful function.

3 As a matter of style, and better maintainability , prefer '<' rather
than '<=' for the control variable test in a for loop:

for (int j = 1; j < length; j++)

Better maintainability because other C# (and C and C++ and Java)
programmers will expect for loops to look like that unless there is a
specific reason not to.

4 You are declaring CurrentCard and i inside a for loop. Probably
better to declare them once just outside the loop rather than each
time inside the loop.

5 Microsoft style guidelines say that you should use an initial lower
case (camelCase) for variables, hence prefer 'currentCard' to
'CurrentCard' etc. An initial capital (PascalCase) is used for
classes, functions and others, so your InsertionSort_F unction is OK
except for the underscore which Microsoft standards discourage.
InsertionSortFu nction would conform to the standard.

rossum
Nov 29 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7129
by: Shaunak Kashyap | last post by:
Does anyone know what sorting algorithm(s) -- quicksort, mergesort, radix sort, etc. -- does PHP use internally in its sort function?
22
4132
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b) { return -1; } else
1
2626
by: aredo3604gif | last post by:
On Sun, 10 Apr 2005 19:46:32 GMT, aredo3604gif@yahoo.com wrote: >The user can dynamically enter and change the rule connection between >objects. The rule is a "<" and so given two objects: >a < b simply means that b < a can't be set, also it must be a != b. >And with three objects a < b , b < c means a < c > >I studied Quick Union Find...
25
11362
by: Allie | last post by:
How would I go about sorting this structure by title? typedef struct { char author; char title; char code; int hold; int loan; } LIBRARY;
4
4271
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event is stored in a double linked list, the whole list is sorted for the next round. My problem appears when subjecting the program to heavy load, that...
4
2164
by: rushik | last post by:
Hello all, I am using structure in my program, and my aim is to sort this structure based on some optimized sorting algo. structure is struct data { int account;
11
2174
by: Registered User | last post by:
What is the name of the following sorting method? for (i=0; i<n-1; i++) for (j=i+1; j<n; j++) if (a>a) swap(&a, &a); It seems to be the opposite of bubble sort, with lighter elements going in their proper positions first. However, this one does not compare a with a, as the normal bubble sort does.
5
1242
by: chrisguest | last post by:
I am trying to write some code that will take a list of functional expressions, and order them so that those with primitive terms appear at the beginning of the list and those that are defined by other terms appear last. eg: getSortedEquations() = It is easy enough to tokenise each of the equations and produce a list
17
402
by: Umesh | last post by:
I don't understand it. Help me by writing a program while i try myself to sort an array of numbers. Thanks.
5
3172
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The search algorithms that can be used are Linear Search and Binary Search. The sorting algorithms are bubble, selection and Insertion sort. First, the user...
0
7776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8121
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8286
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7869
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8143
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6517
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5340
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
1107
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.