This quicksort should sort "v [left] ... v [right] in ascending order"; (no comment) from the C & K programming language (second edition):
void qsort(int v[], int left, int right) { int i, last; void swap(int v[], int i, int j); if (left >= right) return; swap(v, left, (left + right) / 2); last = left; for (i = left+1; i <= right; i++) if (v[i] < v[left]) swap(v, ++last, i); swap(v, left, last); qsort(v, left, last-1); qsort(v, last+1, right); }
I think there is a mistake in
(left + right) / 2
Suppose left = INT_MAX - 1 and right = INT_MAX. Could this lead to undefined behavior due to integer overflow?
functionptr
source share