Created
October 11, 2016 21:25
-
-
Save iburlakov/19dc17a76032d6ff2a5ef7d45e32f569 to your computer and use it in GitHub Desktop.
Quicksort in-place implementation in c#
This file contains hidden or 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
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