Created
May 23, 2016 03:02
-
-
Save gazolla/8a43d9c613f1be2fe5fdf4d1dfd30a8e to your computer and use it in GitHub Desktop.
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>(list:[T])->[T]{ | |
if list.isEmpty { | |
return list | |
} | |
let pivot = list[list.count/2] | |
let equal = list.filter{ $0 == pivot } | |
let less = list.filter{ $0 < pivot } | |
let greater = list.filter{ $0 > pivot } | |
return quicksort(less) + equal + quicksort(greater) | |
} | |
let numbers = [6,5,4,3,2,9,8,7] | |
print(numbers) | |
let ordered = quicksort(numbers) | |
print(ordered) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment