Created
December 12, 2017 11:40
-
-
Save donguri9/770654498f03baf37b77b73b6ebc6101 to your computer and use it in GitHub Desktop.
selectionSort
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(_ 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