Quick actions

cmd+k|ctrl+k

Navigation

Languages

Insertion Sort

Snippet info

Language

JavaScript

Visibility

public

Author

varun.muriyanat

Created

2025-05-31T13:37:28.492339Z

Updated

2025-05-31T14:24:01.183213Z

// let array = [8, 5, 2, 9, 10, 1];
let array = [99,44,6,2,1,5,63,87,283,4,0];
function insertionSort(arr) {
    // debugger;
    for (let i=1; i<arr.length;i++) {
        let temp = arr[i];
        let j = i-1;
        while ((arr[j] > temp) && (j >= 0)) {
            arr[j+1] = arr[j];
            j--;
        }
        arr[j+1] = temp;
    }
    return arr;
}

insertionSort(array);
console.log(array);
INFO