Skip to content

Instantly share code, notes, and snippets.

@obstschale
Created July 23, 2012 13:11
Show Gist options
  • Save obstschale/3163547 to your computer and use it in GitHub Desktop.
Save obstschale/3163547 to your computer and use it in GitHub Desktop.
QuickSort
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