Created
June 22, 2017 03:19
-
-
Save yoxisem544/b2ed2558ea2112611f95c9e300d7357a to your computer and use it in GitHub Desktop.
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
import Foundation | |
extension Array where Element: Comparable { | |
func quickSort() -> [Element] { | |
guard let first = self.first else { return [] } | |
let tail = self.dropFirst() | |
let lessPart = tail.filter { $0 <= first }.quickSort() | |
let morePart = tail.filter { $0 > first }.quickSort() | |
let result = lessPart + [first] + morePart | |
return result | |
} | |
} | |
[7,5,4,4,5,1,8,87,5,4].quickSort() // [1, 4, 4, 4, 5, 5, 5, 7, 8, 87] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment