943,936 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1454
  • C++ RSS
Oct 28th, 2005
0

sorting algorithm not working correctly

Expand Post »
I can't seem to get this sorting alogorithm to work:
C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Oct 28th, 2005
0

Re: sorting algorithm not working correctly

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.

C++ Syntax (Toggle Plain Text)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 28th, 2005
0

Re: sorting algorithm not working correctly

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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.


C++ Syntax (Toggle Plain Text)
  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...
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Oct 29th, 2005
0

Re: sorting algorithm not working correctly

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: homework assignment help
Next Thread in C++ Forum Timeline: queue implementation error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC