Groovy web console

subscribe to the feed Subscribe
to this
site

ProofIT_Integer_Insertion_Sort

Published 3 weeks ago by Jurgis Sipols
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
Integer[] integerArray = [1, 1, -2, 12, 5, 3, 0, 2, 8, 9]
applyInsertionSort(integerArray)
println integerArray

private void applyInsertionSort(Integer[] integerArray) {
    def lastIndex = integerArray.length - 1
    for (i in 0..lastIndex) {
        def integerToMove = integerArray[i]
        def holeIndex = i

        while (holeIndex> 0 && integerArray[holeIndex - 1] > integerToMove) {
            integerArray[holeIndex] = integerArray[holeIndex - 1]
            holeIndex--
        }
            
        integerArray[holeIndex] = integerToMove
    }
}