Skip to content

Instantly share code, notes, and snippets.

@gscalzo
Created June 19, 2014 15:06
Show Gist options
  • Save gscalzo/c1b1a13d72c76101f458 to your computer and use it in GitHub Desktop.
Save gscalzo/c1b1a13d72c76101f458 to your computer and use it in GitHub Desktop.
[Playground] Quicksort implementation in Swift
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"])
@ariok
Copy link

ariok commented Jun 19, 2014

thumbs up!!! ๐Ÿ‘ ๐Ÿ‘

@riffraff
Copy link

no Array#partitionin swift?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment