Insertion Sort - Part 2

  • + 0 comments

    My answer in Typescript, simple

    function insertionSort2(n: number, arr: number[]): void {
        /**
         * again, simple as question request
         * loop [i] from 1, call [insertionSort1] by that sub-array
         * print each loop
         * 
         * note:    [insertionSort1] is answer of previous question 
         *          (return arr instead of print)
         */
        for (let i = 1; i < arr.length; i++) {
            arr = [
                ...insertionSort1(arr.slice(0, i + 1)),
                ...arr.slice(i + 1)
            ]
            console.log(arr.join(' '))
        }
    }