Last active
August 29, 2015 14:02
-
-
Save ariok/8f7f2434a6c80dbd94b5 to your computer and use it in GitHub Desktop.
Quicksort in Swift (Not in-place)
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
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