Buuble sort in javascript

Bubble Sort Javascript

Bubble sort Javascript is one of the sorting algorithms used to sort the elements as per our wish. Sorting algorithms help in rearranging the list of elements or an array into ascending, descending, or required form. We use comparison operators to decide the new order of the list of elements. For example, if we have a list of such as A5, A1, A4, A3, A2 and we have to arrange this list into ascending order. The order of the sorted elements will be A1>A2>A3>A4>A5 in the sequence of ascending order.

There are many algorithms in computer languages that perform this rearrangement such as bubble sort, bucket sort, merge sort, radix sort, insertion sort, selection sort, heap sort, quick sort, counting sort, comb sort etc. Bubble sort is one of the simplest sorting algorithms among all these.

Bubble Sort Algorithm

In this algorithm, we traverse the array from the first element to the last element where the current element is put in comparison with the next element and swapped if the current one is greater than the next element.

Here is a stepwise process of how the user can perform bubble sort; say we have to sort the list in ascending order:

Step 1: we take a list of unsorted elements or an array

For example the list is: 16, 43, 22,78

Step 2: bubble sort takes the first 2 elements 16, 43 and will check for the bigger elements. In this case, 43 is the bigger one and they are already in the sorted locations so the pointer will move forward.

Step 3: now we compare 43 and 22 where 22 is smaller than 43 so the positions will be swapped. Now the list becomes 16, 22, 43, 78

Step 4: As we compare 43 and 78 now, it is again in the sorted fashion. This step doesn’t require swapping anymore.

Step 5: As a result, we get out final list of sorted elements: 16, 22, 43,78

For better understanding, here is a simple bubble sort program in Javascript:

function bubble_Sort(a)
{
    var swapp;
    var m = a.len-1;
    var x=a;
    do {
        swapp = false;
        for (var i=0; i < m; i++)
        {
            if (x[i] < x[i+1])
            {
               var temp = x[i];
               x[i] = x[i+1];
               x[i+1] = temp;
               swapp = true;
            }
        }
        m--;
    } while (swapp);
 return x; 
 }
console.log(bubble_Sort([120, 145, 40, 46, 82, 48, 98, 4, 90, 1, 23, 55, 33, 45, 13]));

The output of this program will be:

1, 4, 13, 23, 33, 40, 45, 46, 48, 55, 82, 90, 98, 120, 145

There’s another way we can simply sort the elements using bubble sort by using sort() method in javascript. The below code will help you in better understanding of the method:

<script> 
function func() { 
var array = [12, 35, 28, 10, 4, 1, 48, 92, 74] 
document.write(array.sort()); 
document.write("<br>");
document.write(array); 
} 
func(); 
</script>

The output of the program will be:

1,10,12,28,35,4,48,74,92

Bubble Sort Complexity

The worst-case and average-case complexity of bubble sort algorithm is O(n^2). Hence this algorithm is not very suitable for large datasets. Here n refers to the number of elements in the list. The worst case is the scenario when the list of elements is the sorted reverse. In the cases where the list of elements is already in a sorted manner, it is a best-case scenario. Auxiliary space of bubble sort is O(1).

Leave a comment

Your email address will not be published. Required fields are marked *