Skip to content

Instantly share code, notes, and snippets.

@alimir1
Last active December 2, 2017 13:08
Show Gist options
  • Save alimir1/0017042b19544970c3e4aaa6df8d2a86 to your computer and use it in GitHub Desktop.
Save alimir1/0017042b19544970c3e4aaa6df8d2a86 to your computer and use it in GitHub Desktop.
Quick Sort in Swift 3
func quickSort<T: Comparable>(array: [T]) -> [T] {
guard !array.isEmpty else { return [] }
let pivot = array.first!
let arrayLessThanOrEqualToPivot = array.dropFirst().filter { $0 <= pivot }
let arrayGreaterThanOrEqualToPivot = array.dropFirst().filter { $0 > pivot }
return quickSort(arrayLessThanOrEqualToPivot) + [Int(pivot)] + quickSort(arrayGreaterThanOrEqualToPivot)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment