Skip to content

Instantly share code, notes, and snippets.

@iburlakov
Created October 11, 2016 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iburlakov/19dc17a76032d6ff2a5ef7d45e32f569 to your computer and use it in GitHub Desktop.
Save iburlakov/19dc17a76032d6ff2a5ef7d45e32f569 to your computer and use it in GitHub Desktop.
Quicksort in-place implementation in c#
int partition(int[] arr, int si, int ei)
{
var pivot = arr[ei];
var pivotIndex = si;
for (var i = si; i < ei; i++)
{
// swap if current is lesser than pivot
if (arr[i] < pivot)
{
var t = arr[pivotIndex];
arr[pivotIndex] = arr[i];
arr[i] = t;
pivotIndex++;
}
}
// swap pivot
arr[ei] = arr[pivotIndex];
arr[pivotIndex] = pivot;
Console.WriteLine(String.Join(" ", arr));
if ((pivotIndex - si ) > 1)
{
partition(arr, si, pivotIndex-1);
}
if (( ei - pivotIndex) > 1)
{
partition(arr, pivotIndex + 1, ei);
}
return pivotIndex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment