Skip to content

Instantly share code, notes, and snippets.

@ocwang
Last active July 23, 2016 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ocwang/408c1072c8461910d18f2c43ff40e6e3 to your computer and use it in GitHub Desktop.
Save ocwang/408c1072c8461910d18f2c43ff40e6e3 to your computer and use it in GitHub Desktop.
func selectionSort(unsortedArray: [Int]) -> [Int] {
var arr = unsortedArray
var temp: Int
for i in 0..<unsortedArray.count {
temp = i
for j in i..<unsortedArray.count {
if arr[j] < arr[temp] {
temp = j
}
}
if temp != i {
swap(&arr[i], &arr[temp])
}
}
return arr
}
func firstTry() {
var index = 0
var tempIndex = 0
while index < unsortedArray.count {
for i in index..<unsortedArray.count {
if unsortedArray[i] < unsortedArray[tempIndex] {
tempIndex = i
}
}
if tempIndex != index {
swap(&unsortedArray[index], &unsortedArray[tempIndex])
}
index += 1
tempIndex = index
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment