Created
July 22, 2016 09:42
-
-
Save mitulmanish/d9ffa4f6b1ad5a927653442bbed8a4cd to your computer and use it in GitHub Desktop.
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(inout set: [Int]) -> [Int] { | |
for i in 0..<set.count - 1 { | |
var smallest = set[i] | |
var currentSmallestIndex = -1 | |
for j in (i+1)..<set.count { | |
if set[j] < smallest { | |
smallest = set[j] | |
currentSmallestIndex = j | |
} | |
} | |
if currentSmallestIndex != -1 { | |
let initial = set[i] | |
set[i] = smallest | |
set[currentSmallestIndex] = initial | |
} | |
} | |
return set | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment