Вопрос

Below are the code snippets of generic qsort on C.

What do I write in the fourth parameter of the genmyqsort when it's called in the recursion?

int compnode(node *a, node *b){
   return(strcmp(a->name,b->name));
}

void genmyqsort(void *a, int n, int size, int (*fcmp)(const void*,const void*)){
  int pivot;

  if(n>1){
    pivot=partition(a,n,size);
    genmyqsort(a*size, pivot,size);
    genmyqsort(a+(pivot+1)*size,n-pivot-1,size);
  }
}

call of Qsort in main.

genmyqsort(b,n,sizeof(node),(int(*)(const void*, const void*)) compnode);
Это было полезно?

Решение

You pass the same comparator as you got from the caller (fcmp):

genmyqsort(a*size, pivot, size, fcmp);
genmyqsort(a+(pivot+1)*size, n-pivot-1, size, fcmp);

This will ensure that all instances of genmyqsort() in the call tree will compare array elements in exactly the same way.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top