Skip to content

Instantly share code, notes, and snippets.

@dzimchuk
Last active December 11, 2015 14:13
Show Gist options
  • Save dzimchuk/84ac9d1b95b195b00db5 to your computer and use it in GitHub Desktop.
Save dzimchuk/84ac9d1b95b195b00db5 to your computer and use it in GitHub Desktop.
public static void QuickSort(IComparable[] elements, int left, int right)
{
int i = left, j = right;
IComparable pivot = elements[(left + right) / 2];
while (i <= j)
{
while (elements[i].CompareTo(pivot) < 0)
{
i++;
}
while (elements[j].CompareTo(pivot) > 0)
{
j--;
}
if (i <= j)
{
// Swap
IComparable tmp = elements[i];
elements[i] = elements[j];
elements[j] = tmp;
i++;
j--;
}
}
// Recursive calls
if (left < j)
{
Quicksort(elements, left, j);
}
if (i < right)
{
Quicksort(elements, i, right);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment