I'm tinkering with a serial quick sort algorithm. Normally I use qsort() but I wanted to test this.
Somehow I loose the highest number. I can't see where I drop it. If someone here could point it out...
Code:
// Help function to quicksort, leave them in front of qsort.
void swap(float_t *left, float_t *right){
float_t temp;
temp = *left;
*left = *right;
*right = temp;
}
// Help function to quicksort, this works like a charm on random dist numbers.
int pivot(int i, int j){
return ((i+j)/2);
}
// Quick sort..
void quick_sort(float_t * array, int low, int high){
int start, end, piv;
float_t k;
if (low < high){
piv = pivot(low,high);
swap(&array[low],&array[piv]);
k = array[low];
start = (low+1);
end = high;
while (start <= end)
{
while ((start <= high) && (array[start] <= k)){ start++; }
while ((end >= low) && (array[end] > k)){ end--; }
if (start < end){ swap(&array[start],&array[end]);}
}
swap(&array[low],&array[end]);
quick_sort(array,low,(end-1));
quick_sort(array,(end+1),high);
}
}