Skip to content

Instantly share code, notes, and snippets.

@mitulmanish
Created July 22, 2016 09:42
Show Gist options
  • Save mitulmanish/d9ffa4f6b1ad5a927653442bbed8a4cd to your computer and use it in GitHub Desktop.
Save mitulmanish/d9ffa4f6b1ad5a927653442bbed8a4cd to your computer and use it in GitHub Desktop.
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