Created
June 17, 2017 13:42
-
-
Save gazolla/4e284a7c03d8cdb44bfcd031a8f07e91 to your computer and use it in GitHub Desktop.
QuickSort created by gazolla - https://repl.it/IkKD/0
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.count == 0 { | |
return [] | |
} | |
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 = [4,3,2,1,5,6,7,8] | |
let ordered = quickSort(numbers) | |
let names=["john", "zack", "tim", "adam", "bill", "peter"] | |
let orderedNames = quickSort(names) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment