How to Write a Bubble Sort Algorithm in JavaScript

The bubble sort algorithm is a simple comparison-based sorting algorithm.

In this algorithm, each element in an array is compared to the adjacent element in the array and then swapped, if required, until all the elements of the array are properly sorted.

In this article, you will learn the basics of bubble sort algorithm and how to write a bubble sort algorithm in JavaScript. I will also discuss the time complexity of the bubble sort algorithm.

Table of Contents

You can skip to a specific section of this JavaScript bubble sort tutorial using the table of contents below:

What is a Bubble Sort Algorithm?

As discussed above, a bubble sort algorithm is a simple comparison-based sorting algorithm where each element of an array is compared to the adjacent element and then swapped if necessary. This process continues until all the elements in the array are sorted as required.

Let’s consider an array with 5 elements: {4, 17, 5, 1, 12} and see how bubble sort algorithm sorts this array in ascending order.

Iteration 1:

{4, 17, 5, 1, 12}    {4, 17, 5, 1, 12} 

The algorithm compares the first two elements and since 4 < 17, does not make any change to the array.

{4, 17, 5, 1, 12}  {4, 5, 17, 1, 12} 

Since 17 > 5, the algorithm swaps the numbers.

{4, 5, 17, 1, 12}   {4, 5, 1, 17, 12}

Since 17 > 1, the algorithm swaps the numbers.

 {4, 5, 1, 17, 12}   {4, 5, 1, 12, 17}

Since 17 > 12, the algorithm swaps the numbers.

Iteration 2:

{4, 5, 1, 12, 17} {4, 5, 1, 12, 17}

Since 4 < 5, the algorithm does not swap the numbers.

{4, 5, 1, 12, 17} {4, 1, 5, 12, 17}

Since 5 > 1, the algorithm swaps the numbers.

{4, 1, 5, 12, 17} {4, 1, 5, 12, 17}

Since 5 < 17, the algorithm does not swap the numbers.

{4, 1, 5, 12, 17} {4, 1, 5, 12, 17}

Since 12 < 17, the algorithm does not swap the numbers.

Iteration 3:

{4, 1, 5, 12, 17} {1, 4, 5, 12, 17}

Since 4 > 1, the algorithm swaps the numbers.

{1, 4, 5, 12, 17} {1, 4, 5, 12, 17}

Since 4 < 5, the algorithm does not swap the numbers.

{1, 4, 5, 12, 17} {1, 4, 5, 12, 17}

Since 5 < 12, the algorithm does not swap the numbers.

{1, 4, 5, 12, 17} {1, 4, 5, 12, 17}

Since 12 < 17, the algorithm does not swap the numbers.

The array is now sorted in ascending order. However, the algorithm is not aware of that and will continue to run until it completes one full iteration without any swapping. Therefore, the fourth iteration will also run.

Writing a Bubble Sort Algorithm With JavaScript

Let’s now look at a simple JavaScript code that runs a bubble sort algorithm.

function bubbleSort(a) 

{

    var length = a.length;

    for (var i = 0; i < length; i++) //Number of iterations

    { 

      for (var x = 0; x < (length - i - 1); x++) //Note that x < (length - i)

 

      { 

        if (items[x] > items[x + 1]) 

        {

          

          var tmp = items[x]; //Temp variable to hold the current number

          items[x] = items[x + 1]; //Replace current number with next number

          items[x + 1] = tmp; //Replace next number with current number

        }

      }

    }

  }

The Time Complexity of a Bubble Sort Algorithm

Bubble sort algorithm is a simple sorting algorithm. Since only one additional memory space (the temp variable in the above example), this algorithm has a space complexity of O(1).

If there are n numbers, the number of comparisons done in each iteration would be as follows:

1st Iteration: (n – 1)

2nd Iteration: (n – 2)

3rd Iteration: (n – 3) and so on

The total number of comparisons = (n – 1) + (n – 1) + (n – 1) + … 3 + 2 + 1 = n(n – 1)/2 +

i.e O(n2)

Therefore, the best-case time complexity for a bubble sort algorithm is O(n), when all the items are already sorted, and the worst-case complexity is O(n2).

Final Thoughts

In this tutorial, you learned how to write a bubble sort algorithm in JavaScript.

Here is a brief summary of what we discussed in this article:

  • How a bubble sort algorithm works
  • How to write a bubble sort algorithm in JavaScript
  • The time complexity of a bubble sort algorithm

Written on June 4th, 2020