Skip to content

Instantly share code, notes, and snippets.

@khawajafarooq
Last active August 4, 2017 13:46
Show Gist options
  • Save khawajafarooq/3d5af39c019d0d9f774039a099a8f087 to your computer and use it in GitHub Desktop.
Save khawajafarooq/3d5af39c019d0d9f774039a099a8f087 to your computer and use it in GitHub Desktop.
Swifty selection sort
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