473,385 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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_Function(uint[] IntCard,int length)
{
for (int j = 1; j <= (length-1); j++)
{
uint CurrentCard=IntCard[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 1805
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_Function(uint[] IntCard,int length)
{
for (int j = 1; j <= (length-1); j++)
{
uint CurrentCard=IntCard[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_Function is OK
except for the underscore which Microsoft standards discourage.
InsertionSortFunction 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_Function(uint[] IntCard,int length)
{
for (int j = 1; j <= (length-1); j++)
{
uint CurrentCard=IntCard[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_Function(uint[] IntCard,int length)
{
for (int j = 1; j <= (length-1); j++)
{
uint CurrentCard=IntCard[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_Function is OK
except for the underscore which Microsoft standards discourage.
InsertionSortFunction 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
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
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)...
1
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 <...
25
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
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...
4
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
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...
5
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...
17
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...

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.