Skip to content

Instantly share code, notes, and snippets.

@lajosdeme
Created February 10, 2020 18:07
Show Gist options
  • Save lajosdeme/379b4c951529d501f9e0c684b88b78ae to your computer and use it in GitHub Desktop.
Save lajosdeme/379b4c951529d501f9e0c684b88b78ae to your computer and use it in GitHub Desktop.
Implementation of insertion sort algorithm in Swift
func insertionSort <T: Comparable>(array: inout [T]) {
if array.isEmpty {
return
}
for i in 1..<array.count {
var pos = i
let temp = array[i]
while pos > 0 && array[pos - 1] > temp {
array[pos] = array[pos - 1]
pos -= 1
}
array[pos] = temp
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment