Created
June 6, 2017 07:59
-
-
Save st-small/57816f8778cba61804185792ef6da3a9 to your computer and use it in GitHub Desktop.
quickSort
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
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