gist:7022556 · GitHub
Skip to content

Instantly share code, notes, and snippets.

@sverrejoh
Created October 17, 2013 10:22
Show Gist options
  • Save sverrejoh/7022556 to your computer and use it in GitHub Desktop.
Save sverrejoh/7022556 to your computer and use it in GitHub Desktop.
void quicksort(int l, int u)
{ int i, m;
// Return if length is 0
if (l >= u) {
return;
}
// Move random element to start
swap(l, randint(l, u));
m = l;
// Iterate all element after start
for (i = l+1; i <= u; i++)
// if element is smaller than pivot, move it to the first section.
if (x[i] < x[l]) {
swap(++m, i);
}
// Swap pivot into last, which is smaller an can then be at beginning.
swap(l, m);
quicksort(l, m-1);
quicksort(m+1, u);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment