문제

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