Created
June 19, 2014 15:06
-
-
Save gscalzo/c1b1a13d72c76101f458 to your computer and use it in GitHub Desktop.
[Playground] Quicksort implementation in Swift
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
func qs<T: Comparable>(ary: T[]) -> T[] { | |
if ary.isEmpty { | |
return [] | |
} | |
let x = ary[0] | |
let xs = ary[1..ary.count] | |
let smallerSorted = qs(Array(filter(xs) { $0 <= x })) | |
let biggerSorted = qs(Array(filter(xs) { $0 > x })) | |
return smallerSorted + [x] + biggerSorted | |
} | |
qs([1,5,3,5,3,2,4,6,7,8,3,7]) | |
qs(["z", "k","a","f"]) | |
qs(["bravo","alpha","aalpha","delta","charlie"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
no
Array#partition
in swift?