Quick actions

cmd+k|ctrl+k

Navigation

Languages

bubble sort

Snippet info

Language

JavaScript

Visibility

public

Author

jack98765.2007

Created

2024-06-18T15:16:26.431548Z

Updated

2024-06-18T16:14:53.762391Z

function bubbleSort(arr) {
    let sorted;
    do {
        sorted = true;
        for (let i = 0; i < arr.length - 1; i++) {
            console.log(...arr);
            console.log('  '.repeat(i) + '^ ^');
            if (arr[i] > arr[i + 1]) {
                sorted = false;
                [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]];
            }
        }
    } while (!sorted);
}

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