Last active
December 2, 2017 13:08
-
-
Save alimir1/0017042b19544970c3e4aaa6df8d2a86 to your computer and use it in GitHub Desktop.
Quick Sort in Swift 3
This file contains 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>(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