i put a "WHOABUDDY" at the bottom of the swap function. it printed the list of names, WHOABUDDY twice, and then the list of names again still unsorted. the two whoabuddy's look odd to me as it is only called once. any thoughts?
i put a "WHOABUDDY" at the bottom of the swap function. it printed the list of names, WHOABUDDY twice, and then the list of names again still unsorted. the two whoabuddy's look odd to me as it is only called once. any thoughts?
Twice does seem a bit low, yes. I'd expect as many swaps as you have iterations, which should be 5.
Do you actually want the red bit?Code:for (pass = 0; pass < SIZE - 1; pass++) { for (min = pass, trav = pass + 1; trav < SIZE; trav++) { if (identity[trav].lName < identity[min].lName) { min = trav; } trav++; } swap(&identity[pass], &identity[min]); pass++; }
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
allllright, now we're gettin somewhere. i took out the pass++; and more whoabuddys came up but it still wasnt swapping. in the call of the swap function, i changed &identity[pass] to &identity[pass + 1] and it swapped only one of the names out of order, whichever one is at the top to its correct place.
are you referring to the trav++; after the if? i'm playing around with all kinds of stuff and cannot figure out which section of the loop you are referring to.
Yes, you have two trav++ in your for-loop, one in the for-statement itself, and one just before the end brace. Whilst this is within the C language specification, I'm 99.9% sure that you do not want to do that.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
hen you use a for loop, you don't have to iterate the variable inside the loop, the setup of the loop does that for you
If you used a while loop then you'd need to increment tha variable inside the loop...Code:for (var = 0; var < MAX; var++) { // Note the var++ here! // Do Something // No var++; needed!!! }
These two loops are exactly the same functionally.Code:var = 0; while (var < MAX) { // Do Something var++; }
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Generally for loops are used when you want to perform the same action a set number of times (like you do in your code). Generally while loops are used when you want to continue to perform a given action until a certain result is obtained. It is a fine, yet clear, distinction.
i removed the second trav++; and it is still only swapping the top name that is out of order to its correct spot.
This is what your sort function should look like:
You never need to increment pass, trav, or anything else, outside the for loops.Code:int pass, trav; for (pass = 0; pass < SIZE - 1; pass++) { for (trav = pass + 1; trav < SIZE; trav++) { if (identity[pass].lName > identity[trav].lName) { swap(&identity[pass], &identity[trav]); } } //end of inner for } //end of outer for
Min is not needed at all.
This is my favorite flavor of bubblesort. Please don't muck around with it.
If you have a lot of empty array slots, they will be the first things you see after the sort, but that doesn't mean the sort is not working.
i tried your code and it didn't swap any at all. im trying to do it with a selection sort instead of bubble if that is at all possible.
Post your code up. I've used that particular version of sorting for almost three decades
It's selection sort, (sort of), but it sweeps all the way through the array on the inner loop, so it falls into the performance of the bubblesort "bunch".
If you want selection sort, that's fine.
You know you have to compare strings in C with strcmp(), right? You can't just compare name to name, directly.
Last edited by Adak; 04-14-2009 at 01:34 PM.