Skip to content

Instantly share code, notes, and snippets.

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