473,737 Members | 8,878 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 1819
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
7138
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
4164
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
2640
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 algorithms a bit and if I understood them >correctly, once the user gives the input setting the...
25
11380
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
4284
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 is, when I run the simulation for more than 10,000 miliseconds (every event occurs in...
4
2171
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
2187
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
1249
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
3186
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 is asked whether he/she wants to perform a search option, a sort operation, or exit the program. If...
0
8968
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8786
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9207
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6749
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6052
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2192
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.