Skip to content

Instantly share code, notes, and snippets.

@SanjeevMohindra
Last active January 2, 2017 03:30
Show Gist options
  • Select an option

  • Save SanjeevMohindra/9c2f470657da39556998772f7b506835 to your computer and use it in GitHub Desktop.

Select an option

Save SanjeevMohindra/9c2f470657da39556998772f7b506835 to your computer and use it in GitHub Desktop.
func insertionSort(inputData: inout [Int]) {
for forwardPointer in 1...inputData.count - 1 {
let selectElement = inputData[forwardPointer] //Select the element
var reversePointer = forwardPointer - 1 //Set the reverse pointer
while (reversePointer >= 0 && selectElement < inputData[reversePointer]) {
inputData[reversePointer + 1] = inputData[reversePointer] //move the value forward
reversePointer -= 1; //decrease the pointer
}
inputData[reversePointer + 1] = selectElement //store the value at correct place
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment