|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Quicksort pivot selection
I am working with a quicksort function. It works fine when I set the pivot to the first element in the array, but when I set it to the last element the array is "semi" sorted with the first element that should be there is replaced with a duplicate of the second, and there are a few other duplicates throughout the array.
Here is the code: //splitting function for quicksort void split(int x[], int first, int last, int & pos) { int left=first; int right=last; int pivot = x[right]; <-------------------works as [left] while(left<right) { while(x[right] > pivot) right--; while(left < right && x[left] <= pivot) left++; if(left < right) swap(x[left], x[right]); } pos=right; x[first] = x[pos]; x[pos] = pivot; } //quicksort function void quicksort2(int x[], int first, int last) { int pos; if(first<last) { split(x,first,last,pos); quicksort2(x,first,pos-1); quicksort2(x,pos+1, last); } } Help is appreciated. Thanks! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Quicksort pivot selection |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|