Hi all,
I have a quick sort function which i'm working on however, I am having problems with it. It seems to get stuck in a loop...when ever i break the program its on my comparison function. I know this function works as i use it in a bubble sort too.
Even with only 10 records to sort it never seem to finnish sorting and out any results.
thanks for any help in the matter, my code is as follows.
Code:
void quickSort( float * dataArray, size_t first, size_t last, int ( *ptrComp ) ( const void * a, const void * b ), void ( *ptrSwap ) ( const void * a, const void * b ) ) {
size_t i, j;
int x, temp;
i = first;
j = last;
x = dataArray[(first + last) / 2];
// Check That There Are At Least Two Elements To Sort
if ( first < last ) {
// While the to partition indexes are apart
do {
// From the left, look for the first
// element grater than the pivot
while (dataArray[i] < x && i < last) {
++i;
}
// from the right, look for the first
// element less than the pivot
while (dataArray[j] > x && j > first) {
--j;
}
// Compare data and swap if needed
if ( ( *ptrComp ) ( &dataArray[i], &dataArray[j] ) == -1 ) {
( *ptrSwap ) ( &dataArray[i], &dataArray[j] );
++i;
--j;
}
} while (i <= j);
// Quicksort the left partition
if (first < j) {
quickSort( dataArray, first, j, ptrComp, ptrSwap );
}
// Quicksort the right partition
if (i < last) {
quickSort( dataArray, i, last, ptrComp, ptrSwap );
}
}
}