Implementation in JavaScript

suggest change
function bubbleSort(values) {
    // Each pass places the largest remaining value at the end.
    for (let end = values.length - 1; end > 0; end--) {
        let swapped = false;
        for (let index = 0; index < end; index++) {
            if (values[index] > values[index + 1]) {
                [values[index], values[index + 1]] = [values[index + 1], values[index]];
                swapped = true;
            }
        }
        if (!swapped) break;
    }
    return values;
}

const values = [3, 203, 34, 746, 200, 984, 198, 764, 9];
console.log(`[${bubbleSort(values).join(", ")}]`);
[3, 9, 34, 198, 200, 203, 746, 764, 984]

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents