Created
July 23, 2012 13:11
-
-
Save obstschale/3163547 to your computer and use it in GitHub Desktop.
QuickSort
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 quickSort(int a[ ], int from, int to) | |
{ // sort partition from ... to of array a | |
int i, pivot, new_val; | |
if (from < to) // at least 2 elements in partition | |
{ | |
pivot = from; | |
for (i = from + 1; i <= to; i++) | |
{ | |
new_val = a[i]; | |
if (new_val < a[pivot]) | |
{ // shift values to the right | |
a[i] = a[pivot+1]; | |
a[pivot+1] = a[pivot]; | |
a[pivot] = new_val; | |
pivot++; | |
} // else a[i] stays put | |
} | |
quickSort(a, from, pivot-1); // left partition | |
quickSort(a, pivot+1, to); // right partition | |
} // else (from >= to) only 0 or 1 elements | |
} // end quicksort |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment