Created
January 3, 2018 13:27
-
-
Save mwolicki/336c27c5c3ece1843ff59329f9df6f2f to your computer and use it in GitHub Desktop.
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
using System; | |
public static class Sort { | |
public static void quicksort(Span<int> arr){ | |
void swap(Span<int> s, int i, int j){ | |
var tmp = s[i]; | |
s[i] = s[j]; | |
s[j] = tmp; | |
} | |
int partition(Span<int> s) { | |
var i=0; | |
var pivot = s[0]; | |
for(int j = 1; j < s.Length; j++){ | |
if(pivot > s[j]) { | |
swap(s, i, j); | |
i++; | |
} | |
} | |
if(pivot > s[s.Length - 1]) { | |
swap(s, 0, s.Length - 1); | |
} | |
return i; | |
} | |
if (arr.Length <= 1) return; | |
var p = partition(arr); | |
if(p-1>0) quicksort(arr.Slice(0, p - 1)); | |
if(p+1<arr.Length) quicksort(arr.Slice(p + 1)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment