Skip to content

Instantly share code, notes, and snippets.

@st-small
Created June 6, 2017 07:59
Show Gist options
  • Save st-small/57816f8778cba61804185792ef6da3a9 to your computer and use it in GitHub Desktop.
Save st-small/57816f8778cba61804185792ef6da3a9 to your computer and use it in GitHub Desktop.
quickSort
list = [9, 6, 7, 3, 42, 2, 78, 4, 5, 67, 98, 1]
func quickSort2 <T: Comparable> (_ list: [T]) -> [T] {
if list.count == 0 {
return []
}
let pivot = list[0]
let sublist = list.count > 1 ? list[1..<list.count] : []
let smaller = sublist.filter{$0 <= pivot}
let greater = sublist.filter{$0 > pivot}
return quickSort2(smaller) + pivot + quickSort2(greater)
}
quickSort2(list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment