Created
November 13, 2015 10:42
-
-
Save sambhav7890/5cb8b17d2c4e2a45dcf0 to your computer and use it in GitHub Desktop.
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>(var 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