Quick actions

cmd+k|ctrl+k

Navigation

Languages

insertion sort

Snippet info

Language

JavaScript

Visibility

public

Author

jack98765.2007

Created

2024-06-18T16:13:38.788845Z

Updated

2024-06-18T16:13:38.788845Z

function insertSort(arr) {
    for (let i = 0; i < arr.length; i++) {
        console.log(...arr);
        console.log('  '.repeat(i) + '^|');
        for (let j = i; j > 0;) {
            if (arr[j] >= arr[j - 1]) {
                break;
            }
            [arr[j], arr[j - 1]] = [arr[j - 1], arr[j]];
            j--;
            console.log(...arr);
            console.log('  '.repeat(j) + '^' + '  '.repeat(i - j) + '|');
        }
    }
}

insertSort([1, 3, 2, 5, 4, 8, 9, 7, 6, 0]);
INFO