简体   繁体   中英

Sorting char** array with generic quicksort in c

I am currently unit testing the generic quicksort I made for which i will post the code right here below, along with the compare method I'm using and the code I'm running to test it.
The problem I find is that the array is not actually being sorted. The assertion fails and if I try to print the array after sorting it just prints the values in the predetermined order.
I tried unit testing the algorithm for int and float values and it did sort just fine.
I believe the problem may lie in the fact that I probably did not cast correctly the void pointer, resulting in undefined behaviour

void quicksort(void* pointer, int size, int start, int end, int (*compare)(void*,void*)){
    if(end > start){
        int index = partition(pointer, size, start, end,compare);
        quicksort(pointer,size,start,index-1,compare);
        quicksort(pointer,size,index+1,end,compare);
    }
}

int partition(void * pointer, int size, int start, int end, int (*compare)(void*,void*)){
    int beginning = start + 1;
    int ending = end;
    while(beginning <= ending){
        if(compare(&pointer[beginning*size],&pointer[start*size]) >= 0){
            beginning++;
        }
        else{
            if(compare(&pointer[ending*size],&pointer[start*size]) == -1){
                ending--;
            }
            else{
                swap(&pointer[beginning*size],&pointer[ending*size],size);
                beginning++;
                ending--;
            } 
        }
    }
    swap(&pointer[start*size],&pointer[ending*size],size);
    return ending;
}

void swap(void* datablocka, void* datablockb, size_t elem_size){
    char temp;
    char *chara = datablocka;
    char *charb = datablockb;
    while(elem_size--){
        temp = chara[elem_size];
        chara[elem_size] = charb[elem_size];
        charb[elem_size] = temp;
    }
}

int compare_string(void* first, void* second){
    char * a = first;
    char * b = second;
    int compare = strcmp(a,b);
    if (compare < 0) return 1;
    else if(compare == 0) return 0;
    else return -1;
}

void quicksort_string_utest() {
  char* a[] = {"a","aaa","abba","mime","pippo","cacciatorpediniere"};
  char* b[] = {"a","aaa","abba","cacciatorpediniere","mime","pippo"};
  int end = (sizeof(a)/sizeof(a[0]))-1;
  quicksort(a, sizeof(a[0]), 0, end, compare_string );
  for(int i = 0; i < 6; i++){
    assert(strcmp(a[i],b[i]) == 0);
  }
}

I would really appreciate your help with this, since as you can see I can't sort this out

In this program, what is passed to the comparision function compare_string is pointers to elements of the array.

In this case, elements of the array is char* and what is passed is pointers to char* .

Therefore, to obtain the pointers to strings, you have to dereference the passed pointers.

Try this:

int compare_string(void* first, void* second){
    char ** a = first;
    char ** b = second;
    int compare = strcmp(*a,*b);
    if (compare < 0) return 1;
    else if(compare == 0) return 0;
    else return -1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM