You sort one half with:
qsort(array, 0, k);
and similarly you need the other half:
qsort(array+k, 0, array_length-k);
Now the problem is that both parts will be in ascending order. Therefore, you need to specify qsort() to sort one half in ascending order and the other half in descending order. Pass another flag to qsort() to change the swap order. So you can specify bool to specify it:
void qsort(int arr[], int fst, int last, bool pass) { .... if (pass && i < j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } if(!pass && i > j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } ... qsort(arr, fst, j - 1, pass); qsort(arr, j + 1, last, pass);
}
And when you call it, you can pass true and false to "switch" the swap order:
qsort(array, 0, k, true); qsort(array+k, 0, array_length-k, false);
Change the qsort() prototype accordingly.
source share