DEV Community

Cover image for Bubble Sort again with JavaScript
sk9
sk9

Posted on

Bubble Sort again with JavaScript

Below code is a sample of the Bubble Sort algorithm written in JavaScript.

function bSort(a) { 
    for (let i in a) {
        for (let j = a.length - 1; j > i; j--) {
            if (a[j] < a[j - 1]) { 
                const b = a[j - 1];
                a[j - 1] = a[j];
                a[j] = b;
            } 
        } 
    } 
    return a;
}

bSort([5,3,2,1,4]); // 1, 2, 3, 4, 5

That's really easy ☺

But how many people know how it works?

Ok. That's enough...

  • Many people know what is Bubble Sort algorithm
  • Many people know how to write JavaScript

There's nothing I can do about it 😇


However ...

If you would like to know again how to write Bubble Sort algorithm in JavaScript,
please watch my video 😉

Top comments (0)