Skip to content

Instantly share code, notes, and snippets.

@donguri9
Created December 12, 2017 11:40
Show Gist options
  • Save donguri9/770654498f03baf37b77b73b6ebc6101 to your computer and use it in GitHub Desktop.
Save donguri9/770654498f03baf37b77b73b6ebc6101 to your computer and use it in GitHub Desktop.
selectionSort
func selectionSort(_ array:[Int])->[Int]{
guard array.count > 1 else {
return array}
var a = array
for x in 0..<a.count - 1{
var lower = x
for y in x + 1..<a.count{
if a[y] < a[lower]{
lower = y
}
}
if x != lower{
a.swapAt(x, lower)
}
}
return a
}
var arr = [3,1,8,2,5,1]
selectionSort(arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment