Last active
January 18, 2019 08:35
-
-
Save renaudcerrato/8c0bd3b9af5a8156ef90d2f88913c9a5 to your computer and use it in GitHub Desktop.
Nested Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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