Site icon Ravi Shankar

Insertion Sort

Insertion Sort algorithm does the following

Here is code snippet of Insertion Sort in Swift.

[code language=”swift”]var inputArr:[Int] = [Int]()

// generate random numbers
for rIndex in 0..<10 {
inputArr.append(((Int(arc4random()) % 100)))
}

func insertionSort(var inputArray :[Int]) -> [Int] {
var jIndex:Int,kIndex:Int

for kIndex in 1.. 0 && inputArray[jIndex-1] >= temp ) {
inputArray[jIndex] = inputArray[jIndex-1]
–jIndex
}
inputArray[jIndex] = temp
}

return inputArray
}

insertionSort(inputArr)[/code]

Exit mobile version