Skip to content

Instantly share code, notes, and snippets.

@gazolla
Created June 17, 2017 13:42
Show Gist options
  • Save gazolla/4e284a7c03d8cdb44bfcd031a8f07e91 to your computer and use it in GitHub Desktop.
Save gazolla/4e284a7c03d8cdb44bfcd031a8f07e91 to your computer and use it in GitHub Desktop.
QuickSort created by gazolla - https://repl.it/IkKD/0
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