Created
November 29, 2020 19:58
-
-
Save 0xMarK/1feafd19f9f2d57e4bd5e8605a59e1e5 to your computer and use it in GitHub Desktop.
Selection Sort
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 selectionSort(_ array: [Int]) -> [Int] { | |
guard array.count > 1 else { return array } | |
var sorted = array | |
var smallestIndex = 0 | |
for i in 0..<array.count { | |
for j in (i + 1)..<array.count { | |
if sorted[j] < sorted[smallestIndex] { | |
smallestIndex = j | |
} | |
} | |
let smallest = sorted[smallestIndex] | |
sorted[smallestIndex] = sorted[i] | |
sorted[i] = smallest | |
} | |
return sorted | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment