Skip to content

Instantly share code, notes, and snippets.

@chriswebb09
Created April 30, 2017 04:27
Show Gist options
  • Save chriswebb09/09be42ac7d2b1b46413e7afed9db92f2 to your computer and use it in GitHub Desktop.
Save chriswebb09/09be42ac7d2b1b46413e7afed9db92f2 to your computer and use it in GitHub Desktop.
import UIKit
func insertionSort(values: [Int]) {
var valuesArray = values
for i in 1...valuesArray.count - 1 {
let nextItem = valuesArray[i]
var currentIndex = i - 1
while currentIndex >= 0 && valuesArray[currentIndex] > nextItem {
valuesArray[currentIndex + 1] = valuesArray[currentIndex]
currentIndex -= 1
}
valuesArray[currentIndex + 1] = nextItem
}
}
insertionSort(values: [1, 4, 5, 2, 3, 7, 8, 6, 10, 9])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment