Skip to content

Instantly share code, notes, and snippets.

@ariok
Last active August 29, 2015 14:02
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 ariok/8f7f2434a6c80dbd94b5 to your computer and use it in GitHub Desktop.
Save ariok/8f7f2434a6c80dbd94b5 to your computer and use it in GitHub Desktop.
Quicksort in Swift (Not in-place)
import Cocoa
func quicksort(array:Int[])->Int[] {
var less = Int[](), equal = Int[](), greater = Int[]()
if array.count > 1{
let pivot = array[0]
for x in array{
switch(x){
case (let v) where v < pivot:
less.append(v)
case (let v) where v > pivot:
greater.append(v)
default:
equal.append(x)
}
}
return (quicksort(less) + equal + quicksort(greater));
}else{
return array;
}
}
var random = [22,19,3,1,123];
var sorted = quicksort(random);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment