vendredi 3 avril 2015

Quicksort with strings and strcmp in C

I need to rewrite a simple quicksort in order to work with strings. Using strcmp and passing a strings matrix, it doesn't always works fine and exit without loops, but i don't know what's wrong with it.


To avoid some loops i put && i < high into the while, it temporarily solve many loops but it doesn't explain why it goes after high.



void quickSortForLists(int low, int high, char **matList){
int i, j;
char *pivot, *tempBuffer;
i=low; j=high;
pivot=matList[(low+high)/2];
do
{

while (strcmp(matList[i], pivot) < 0 && i<high) ++i;
while (strcmp(matList[j], pivot) > 0) --j;
if(i<=j){
tempBuffer=matList[i];
matList[i]=matList[j];
matList[j]=tempBuffer;
++i; --j;
}
} while (j>=i);
if(low<j) quickSortForLists(low,j,matList);
if(i<high) quickSortForLists(i,high,matList);


}


Aucun commentaire:

Enregistrer un commentaire