Skip to content

Instantly share code, notes, and snippets.

@achernoprudov
Created May 12, 2019 09:39
Show Gist options
  • Save achernoprudov/844123777effc1b2f40c4d47d572dea0 to your computer and use it in GitHub Desktop.
Save achernoprudov/844123777effc1b2f40c4d47d572dea0 to your computer and use it in GitHub Desktop.
Swift Quicksort.
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