Last active
June 15, 2017 11:59
-
-
Save StevenMasini/b5fff1a3effebef764ca052d4ace31ce to your computer and use it in GitHub Desktop.
My implementation of the Quicksort algorithm in Swift
This file contains hidden or 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>(_ arr: [T]) -> [T] { | |
guard arr.count > 1 else { | |
return arr | |
} | |
let pivot = arr[arr.count / 2] | |
var left = [T]() | |
var equal = [T]() | |
var right = [T]() | |
for i in 0 ..< arr.count { | |
if arr[i] < pivot { | |
left.append(arr[i]) | |
} else if arr[i] == pivot { | |
equal.append(arr[i]) | |
} else { | |
right.append(arr[i]) | |
} | |
} | |
return quicksort(left) + equal + quicksort(right) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment