Created
April 30, 2017 04:27
-
-
Save chriswebb09/09be42ac7d2b1b46413e7afed9db92f2 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
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