| | |
sorting algorithm not working correctly
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
I can't seem to get this sorting alogorithm to work:
It seems to do everything right except the very last number is not in place. Can anyone spot the problem?
C++ Syntax (Toggle Plain Text)
#include <iostream> void sortAssending(int nums[], int size); int main() { int nums[] = {175,167,160,164,183,187,188,179,176,175, 169,175,176,178,165,160,173,165,187,178}; sortAssending(nums, 20); for (int i=0; i<20; i++) { std::cout << nums[i] << std::endl; } system("PAUSE"); } void sortAssending(int nums[], int size) { for (int start=0; start<size; start++) { for (int end=size-1; end>=0; end--) { if (nums[start] > nums[end]) { int temp = nums[start]; nums[start] = nums[end]; nums[end] = temp; } } } }
It seems to do everything right except the very last number is not in place. Can anyone spot the problem?
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)
void sortAssending(int nums[], int size) { for (int start=0; start< (size-1); start++) { for (int end=start+1; end < size; end++) { if (nums[start] > nums[end]) { int temp = nums[start]; nums[start] = nums[end]; nums[end] = temp; } } } }
C++ Syntax (Toggle Plain Text)
if (nums[start] > nums[end]) { int temp = nums[start]; nums[start] = nums[end]; nums[end] = temp; }
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)
2 3 4 1 7 start 6 8 5 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)
2 3 4 1 5 start 6 ^ 8 | 7 end 9
Then 'end' reaches the position just before 'start'. Since nums[start] > nums[end], the two are swapped.
C++ Syntax (Toggle Plain Text)
2 3 4 5 end (after swapping) 1 start 6 8 7 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)
2 3 4 5 1 6 start 8 7 9 end
And it happens all over again...
All my posts may be redistributed under the GNU Free Documentation License.
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
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.
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.
![]() |
Similar Threads
- What is the fastest sorting algorithm? (IT Professionals' Lounge)
- help with sorting arrays from hightest to lowest (C++)
- Can you guys help me? about Quick Sort Algorithm (C)
- CD-ROm isnt working correctly (Storage)
- sorting file (C)
Other Threads in the C++ Forum
- Previous Thread: blink a text??
- Next Thread: queue implementation error
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






