Just working my way through my firs book on visual c# (for dummies) and have come across a bubble sort that makes no sense to me whatsoever.
It sorts planets into order of small to large in terms of length of name.
starts with mecury, venus, earth mars, jupiter
then earth, mecury, venus, mars, jupiter,
then earth, mars, mecury, venus, jupiter,
then earth, jupiter, mars, mecury, venus.
1. Not sure what is meant by length of name as mars, earth, venus, mecury, jupiter? don't understnad what is mean't by length of name as the sort is on array.length which brings back the size of the string i.e. mars would be 4?, earth 5? etc
2. Also in a bubble sort dosn't it start with items 0 and 1 and then 1 and 2 and then 2 and 3 etc comapred and possibly swapped. If so why does jupiter move on the last iteration of the bubble sort. Shoudln't it have moved up one place on the 1st sort i.e. 3 and 4 compared.
3. The book also states that the final pass terminates the sort because nothing changes but the code does not show any sort of if statement that would terminate if two sorts are the same and I presumed that the sort just carried on until all 'for' (inner/outer) iterations had been run.
4. The book also states that if two planets are found out of order then the code 'flags' the fact. Dont understnad what it means by flags the fact other than it swaps the actual planets around. The code does not show any other means of flagging that the planets are not in the correct order.
code example
for (outer = planets.length - 1; outer >= 0; outer--)
{
for (inner = 1; inner <= outer; inner++)
if (planets [inner -1].length > planets[inner].length)
{
//temp store planet
}
}
Code makes sense but the output being produced does not?:mad:
bubble sort
Started by phil333, May 28 2010 12:06 AM
3 replies to this topic
#1
Posted 28 May 2010 - 12:06 AM
|
|
|
#2
Posted 28 May 2010 - 01:20 AM
Ok, the bubble sort works like this:
You go through the array using 2 "for" loops.
And yes, by length it meant the string length: Mars = 4 in length, Earth = 5 in length, etc...
You go through the array using 2 "for" loops.
for (int i = 0; i<array.Lenght; i++)
for (int j = 0; j<array.Lenght; j++)
Now you check if the number that you are at, "i" is greater then the one that the second loop is at "j".
if (array[i] > array[j]) {
If it is bigger, we assign to array[j] the value of array[i]. To do that, we need an auxiliary variable:aux = array[i]; array[i] = array[j]; array[j] = aux; }And that just swapped the values in the array. I hope you understood, I'd need a drawing to explain you better. All you are doing is to compare one value (array[i]) to all the others (array[j]).
And yes, by length it meant the string length: Mars = 4 in length, Earth = 5 in length, etc...
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics
#3
Posted 28 May 2010 - 04:21 AM
Thats the issue though. If the case as you say then why is the end sort ending with
earth, jupiter, mars, mecury, venus. (says in c# dummies book) instead of
mars, earth, venus, mecury, jupiter?
earth, jupiter, mars, mecury, venus. (says in c# dummies book) instead of
mars, earth, venus, mecury, jupiter?
#4
Posted 28 May 2010 - 04:50 AM
Paste me your code.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics


Sign In
Create Account

Back to top









