static void * mergeSortRecur(void *base, size_t nmemb, size_t size, int(*cmp)(const void * p1, const void *p2)) { if (nmemb > 1) { int half = nmemb / 2; void *merged = merge(mergeSortRecur(base, half, size, cmp), half, \ mergeSortRecur(base + half * size, nmemb - half, size, cmp), nmemb - half, size, cmp); copy(base, merged, nmemb*size); free(merged); } return base; }
void mergeSortRecur(int *a, int low, int high, int *endResult) { int mid; if(low<high) { mid=(low+high)/2; mergeSortRecur(a,low,mid,endResult); mergeSortRecur(a,mid+1,high,endResult); merge(a,low,high,mid,endResult); } }
void mergeSort(int *a, int n, int *endResult) { mergeSortRecur(a, 0, n-1, endResult); }
void mergeSort(void *base, size_t nmemb, size_t size, int(*cmp)(const void * p1, const void *p2)) { mergeSortRecur(base, nmemb, size, cmp); }