Last active
June 14, 2016 13:49
-
-
Save dreampiggy/10f7ec8825d78fc71cd9 to your computer and use it in GitHub Desktop.
QuickSort in Swift
This file contains 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 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