Skip to content

Instantly share code, notes, and snippets.

@dreampiggy
Last active June 14, 2016 13:49
Show Gist options
  • Save dreampiggy/10f7ec8825d78fc71cd9 to your computer and use it in GitHub Desktop.
Save dreampiggy/10f7ec8825d78fc71cd9 to your computer and use it in GitHub Desktop.
QuickSort in Swift
func quicksort<T: Comparable>(a: [T]) -> [T] {
if a.count <= 1 {
return a
} else {
let pivot = a[a.count/2]
let less = a.filter { $0 < pivot }
let equal = a.filter { $0 == pivot }
let greater = a.filter { $0 > pivot }
return quicksort(less) + equal + quicksort(greater)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment