Last active
August 4, 2017 13:46
-
-
Save khawajafarooq/3d5af39c019d0d9f774039a099a8f087 to your computer and use it in GitHub Desktop.
Swifty selection sort
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 selectionSort<T: Comparable>(_ a:[T]) -> [T] { | |
guard array.count > 1 else { return array } | |
var array = a | |
for i in 0..<array.count { | |
if let min = Array(array[i..<array.count]).min() { | |
if let idx = array.index(of: min) { | |
array.swapAt(i, idx) | |
} | |
} | |
} | |
return array | |
} | |
var array = [8, 5, 4, 2, 1] | |
selectionSort(array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment