Created
February 10, 2020 18:07
-
-
Save lajosdeme/379b4c951529d501f9e0c684b88b78ae to your computer and use it in GitHub Desktop.
Implementation of insertion sort algorithm in Swift
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
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