Skip to content

Instantly share code, notes, and snippets.

@gazolla
Created May 23, 2016 03:02
Show Gist options
  • Save gazolla/8a43d9c613f1be2fe5fdf4d1dfd30a8e to your computer and use it in GitHub Desktop.
Save gazolla/8a43d9c613f1be2fe5fdf4d1dfd30a8e to your computer and use it in GitHub Desktop.
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