Skip to content

Instantly share code, notes, and snippets.

@yoxisem544
Created June 22, 2017 03:19
Show Gist options
  • Save yoxisem544/b2ed2558ea2112611f95c9e300d7357a to your computer and use it in GitHub Desktop.
Save yoxisem544/b2ed2558ea2112611f95c9e300d7357a to your computer and use it in GitHub Desktop.
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