Skip to content

Instantly share code, notes, and snippets.

@renaudcerrato
Last active January 18, 2019 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renaudcerrato/8c0bd3b9af5a8156ef90d2f88913c9a5 to your computer and use it in GitHub Desktop.
Save renaudcerrato/8c0bd3b9af5a8156ef90d2f88913c9a5 to your computer and use it in GitHub Desktop.
Nested Function
void sort(int *a, unsigned size) {
void quick_sort(unsigned first, unsigned last) {
void swap(unsigned i, unsigned j) {
int tmp = a[i]; a[i] = a[j]; a[j] = tmp;
}
int partition() {
int pivot = a[first];
unsigned index = first;
swap(index, last);
for (unsigned i = first; i < last; i++) if (a[i] < pivot) swap(index++, i);
swap(index, last);
return index;
}
if (first < last) {
unsigned pivotIndex = partition();
quick_sort(first, pivotIndex - 1);
quick_sort(pivotIndex + 1, last);
}
}
quick_sort(0, size - 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment