Last active
December 15, 2016 07:20
-
-
Save trilliwon/012479ea56bafd32e468d2e028795492 to your computer and use it in GitHub Desktop.
Quicksort in Swift
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 | |
func quicksort<T: Comparable>(array: [T]) -> [T] { | |
if array.count <= 1 { return array } | |
let pivot: T = array[array.count/2] | |
return quicksort(array: array.filter ({ $0 < pivot })) + array.filter ({ $0 == pivot }) + quicksort(array: array.filter ({ $0 > pivot })) | |
} | |
// http://www.slideshare.net/ssuserae280e/quicksort-in-swift |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment