Created
May 12, 2019 09:39
-
-
Save achernoprudov/844123777effc1b2f40c4d47d572dea0 to your computer and use it in GitHub Desktop.
Swift Quicksort.
This file contains hidden or 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 <= 1 { | |
return list | |
} | |
let pivot = list[0] | |
var smallerList = [T]() | |
var equalList = [T]() | |
var biggerList = [T]() | |
for x in list { | |
switch x { | |
case let x where x < pivot: | |
smallerList.append(x) | |
case let x where x == pivot: | |
equalList.append(x) | |
case let x where x > pivot: | |
biggerList.append(x) | |
default: | |
break | |
} | |
} | |
return quicksort(smallerList) + equalList + quicksort(biggerList) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment