October 20, 2021

# how bubble sort works We will explore how bubble sort works step by step. Bubble sorting is the simplest sort algorithm. It loops  on the whole of the
Array elements and swaps if one is bigger than the other. We will use a swap function to switches two array element
values.

## Bubble sort algorithm pseudocode

1. Gets the input array that is supposed to be sorted in ascending order.
2. Start a loop with the first element and index “i” equal to the array length.  The loop will start from the ending array to the starting point.
3. create a nested for loop with “j” equals to “i – 1”,  this will start from “0” indexes and loop to the index “i”;
4. Next, we compare the array of index “j” and index “i” if the previous is greater than the latter, then we do the swap.
5. After the first loop, we should return the sorted array.

## Bubble sort example

``````/* Start a loop with the first element and index "i" equal to the array length */
function bubbleSort(arr){
for (var i = arr.length; i > 0; i--) {

}
return arr;
}``````
``````/* create a nested for loop with "j" equals to "i - 1"*/

function bubbleSort(arr){
for (var i = arr.length; i > 0; i--) {
for (var j = 0; j < = i -1;  j++) {

}
}
return arr;
}``````
``````/* Next, we compare the array of index "j" and index "i"  */

function bubbleSort(arr){
for (var i = arr.length; i > 0; i--) {
for (var j = 0; j <=  i -1;  j++) {
if (arr[j]  >  arr[i])  {

}
}
}
return arr;
}``````
``````/* if array of j is greater than array i, we do the swap */

function bubbleSort(arr){
for (var i = arr.length; i > 0; i--) {
for (var j = 0; j <=  i -1;  j++) {
if (arr[j]  >  arr[i])  {
var temp  =  arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
return arr;
}``````
``````/*  finally, we return the sorted array */

function bubbleSort(arr){
for (var i = arr.length; i > 0; i--) {
for (var j = 0; j <=  i -1;  j++) {
if (arr[j]  >  arr[i])  {
var temp  =  arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
return arr;
}

bubbleSort([3,45,9,8])          // [3, 8, 9, 45]
``````
``````/*  with helper swap function*/
var swap = function(array, firstIndex, secondIndex){
var temp = array[firstIndex];
array[firstIndex] = array[secondIndex];
array[secondIndex] = temp;
};

function bubbleSort(arr){
for (var i = arr.length; i > 0; i--) {
for (var j = 0; j <=   i -1;  j++) {
if (arr[j]  >  arr[i])  {
swap(arr, j, i);
}
}
}
return arr;
}

bubbleSort([3,45,9,8])          // [3, 8, 9, 45]``````

## Bubble sort algorithm in data Structure

Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The loop of the list is repeated until the list is sorted.

## Bubble sort time complexity

Time Complexity: O(n 2 )
Space Complexity: O(1)

The bubble sort is the worst sort because it compares all possible pairs.
Other classification algorithms make use of the pre-classified part of the matrix. while
Bubble classification uses nested loops and the time complexity is O (n 2). #### Simon Jerry

View all posts by Simon Jerry →