sorting algorithm not working correctly

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

sorting algorithm not working correctly

 
0
  #1
Oct 28th, 2005
I can't seem to get this sorting alogorithm to work:
  1. #include <iostream>
  2.  
  3.  
  4. void sortAssending(int nums[], int size);
  5.  
  6. int main()
  7. {
  8. int nums[] = {175,167,160,164,183,187,188,179,176,175,
  9. 169,175,176,178,165,160,173,165,187,178};
  10. sortAssending(nums, 20);
  11. for (int i=0; i<20; i++)
  12. {
  13. std::cout << nums[i] << std::endl;
  14. }
  15.  
  16. system("PAUSE");
  17. }
  18.  
  19. void sortAssending(int nums[], int size)
  20. {
  21. for (int start=0; start<size; start++)
  22. {
  23. for (int end=size-1; end>=0; end--)
  24. {
  25. if (nums[start] > nums[end])
  26. {
  27. int temp = nums[start];
  28. nums[start] = nums[end];
  29. nums[end] = temp;
  30. }
  31. }
  32. }
  33. }



It seems to do everything right except the very last number is not in place. Can anyone spot the problem?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,379
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: sorting algorithm not working correctly

 
0
  #2
Oct 28th, 2005
The two loops are not correct. Outer-most loop runs from 0 to (start-1). Inner-most loop runs from (outer_loop_counter+1) to start. There are other ways to do it, but I find this algorithm the most logical (easiest to comprehend) because neither loop counts backwards.

  1. void sortAssending(int nums[], int size)
  2. {
  3. for (int start=0; start< (size-1); start++)
  4. {
  5. for (int end=start+1; end < size; end++)
  6. {
  7. if (nums[start] > nums[end])
  8. {
  9. int temp = nums[start];
  10. nums[start] = nums[end];
  11. nums[end] = temp;
  12. }
  13. }
  14. }
  15. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: sorting algorithm not working correctly

 
0
  #3
Oct 28th, 2005
  1. if (nums[start] > nums[end])
  2. {
  3. int temp = nums[start];
  4. nums[start] = nums[end];
  5. nums[end] = temp;
  6. }

Since end can iterate down to zero, the comparison can result in swappage in the wrong direction. Here's what happens.

When this algorithm is run, the value 'start' ends up dragging up the least element with it. That is, in the middle of your algorithm, your array might look something like this:
  1. 2
  2. 3
  3. 4
  4. 1
  5. 7 start
  6. 6
  7. 8
  8. 5
  9. 9 end

Then, while 'end' iterates downwards, it puts the smallest element between 'start' and 'end' into the position beneath 'start'. In this case, it swaps 5 with 7, and no more swaps are made.

  1. 2
  2. 3
  3. 4
  4. 1
  5. 5 start
  6. 6 ^
  7. 8 |
  8. 7 end
  9. 9

Then 'end' reaches the position just before 'start'. Since nums[start] > nums[end], the two are swapped.

  1. 2
  2. 3
  3. 4
  4. 5 end (after swapping)
  5. 1 start
  6. 6
  7. 8
  8. 7
  9. 9

Then 'end' reaches -1 with no further swapping (because nums[start] contains the minimum element), leaving the beginning region sorted. Then the loop restarts, with 'start' incremented.


  1. 2
  2. 3
  3. 4
  4. 5
  5. 1
  6. 6 start
  7. 8
  8. 7
  9. 9 end

And it happens all over again...
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: sorting algorithm not working correctly

 
0
  #4
Oct 29th, 2005
Oh, Ok. I see what's happening now. You have to start the first loop at zero, and end at a value one less than the size, and for the inner loop start at 1, end at zero. However, I would have never caught that without you guys.
Thanks for all the help!

Dragon, thanks for showing a better alogorithm, and rash, thanks for such a detailed explanation of what I was doing wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC