Bubble sort algorithm is one of the most commonly used algorithms and the most basic one. Bubble sort algorithm is one of the very first algorithms students are taught at schools and courses. For me, I got introduced to this algorithm in basic C/C++ coding course on coursera. Now, as javascript is one of the most used website development languages, I’m writing this tutorial on ‘Bubble sort in Javascript’. You can use Bubble Sort Javascript to build entertaining games for kids.

**Idea: Build a kindergarten games website online.**

Let’s start the tutorial with an easy to understand visual realisation of the algorithm.

# Visual Understanding of Bubble Sort Algorithm

The basic concept of the Bubble Sort algorithm can be visualised below.

Let’s assume, you are given an array of 8 numbers and asked to use the Bubble Sort algorithm to sort the array in ascending order.

The** first step** of the algorithm will be to compare to compare the first two numbers of the array. If the first number in the comparison is larger than the second number, the numbers will swap their positions. If the first number in the comparison is smaller than the second number, there will be no change/swap in their positions.

In the above case, the first number of the comparison pair is larger than the second number, the numbers swap their positions. For **the second step,** numbers 8 and 3 are compared. In this similar pattern of comparison, The numbers are compared and the largest number is fixed at the last position of the array.

Finally, at the end of all the steps and passes, the array is sorted in the ascending order. This is how the Bubble Sort algorithm works in javascript or any other coding language.

You can also use VisualGo.net for visualisation of the bubble sort algorithm. You can vary different sets of parameters and analyse which parameters change which part of the results. This is one of the best ways to understand how all sorting algorithms work.

# The complexity of Bubble Sort in Javascript

The complexity of an algorithm depends on the loops and other parameters and doesn’t depend on the coding language.

Calculating the complexity of any code or algorithm is easy. We need to calculate the number of steps needed to perform a certain task.

For example for bubble sort, we need to perform N steps to fix the largest number at the end. For the second pass or second largest number, we need to perform N-1 steps. So accordingly, The number of steps are:

N + (N-1) + (N-2) + … ≈ (N * (N-1)) / 2 (sum of N natural numbers)

For N → ∞: Number of steps ≈ N². So, the complexity of the algorithm is O(N²).

# Bubble Sort Javascript code

Bubble sort is one of the easiest to code sorting algorithms. It is very easy to code if you can build the logic. With a solid understanding of the logic, you can easily crack bubble sort javascript. The algorithm code is given below:

function bubbleSort(items) {

var length = items.length;

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

for (var j = 0; j < (length – i – 1); j++) { //Notice that j < (length – i) //Compare the adjacent positions if (items[j] > items[j + 1]) {//Swap the numbers

var tmp = items[j]; //Temporary variable to hold the current number

items[j] = items[j + 1]; //Replace current number with adjacent number

items[j + 1] = tmp; //Replace adjacent number with current number

}}}}

## Leave a Reply