Bubble sort Algorithm | bubble sort Time complexity |

Introduction

The bubble sorting algorithm is one of the easiest types of sorting algorithm. In this, we need  to exchange the adjacent elements of an array if they are not in the correct sequence  either we have to set increasing order or decreasing order


The Essence of Bubble Sort

Bubble Sort, a fundamental sorting algorithm, epitomizes simplicity in sorting methodologies. Its approach, akin to the motion of bubbles rising to the surface in water, gradually reorders a list by comparing adjacent elements and swapping them if they are out of order. This iterative process is akin to a series of passes through the list, gradually pushing the larger elements towards the end while smaller elements rise towards the beginning.


The essence of Bubble Sort lies in its systematic traversal through the list, starting from the beginning. At each step, it meticulously examines adjacent elements, ensuring that they are in the correct order. Should the current element be greater than its adjacent counterpart, they elegantly exchange places, aligning themselves in the sorted sequence. This dance continues until every element finds its rightful position.


Why Bubble Sort is Important

While seemingly straightforward, Bubble Sort's elegance emerges from its simplicity. Each iteration through the list contributes to its gradual transformation, akin to the meticulous process of refining a work of art. As the algorithm progresses, the list undergoes a metamorphosis, with disorder yielding to order in a series of graceful exchanges.



Bubble Sort Algorithm In Action


 Let's say we have an array of having  in the below example ;



let array=[2, 5, 6, 9, 4, 3, 7, 8, 1];

for(let i=0; i<=array.length-2; i++){

for(let j=0; j<=array.length-1-i; j++){

if(array[j]>array[j+1]){
temp=array[j+1];
array[j+1]=array[j];
array[j]=temp
}

}

}
console.log(array)



Here, array.length is the length of the array, which is 9. We run a nested loop to compare and swap adjacent elements if they are out of order.

Detailed Explanation


here I am running a nested loop 

and checking the statement  if array[j]  is greater than arr[j+1]  is true then the element gets swapped with its adjacent element 

here my first loop is running till array.length-2 because I am comparing the elements so it will go up to an index of  7; that is where my element is 8;

my second loop is running till the array.length-1-i; 
 suppose if my i=0; 
                     array.length-1=8;
                      i=0;
                       j=array.length-1-i
                      j= 7;

so every time my i increases at the same time the  j last value decreases;

if my i=0 then j<=array.length-1-i; that is 8;
if my i=1; then j<=array.length-1-i; that is 7;
if my i=2; then j<=array.length-1-i; that is 6; 
and so on........ 


1. This is an unsorted array ;




2. when i=0;

  



3. when i=1;






4. when i=2;



 

5. when i=3;


bubble sort


6. when i=4;





7. when i=5;



8. when i=6;




9 when i=7;

  here we get our final sorted array in increasing order ;





Bubble Sort in Decreasing Order 



let array=[2, 5, 6, 9, 4, 3, 7, 8, 1];

for(let i=0; i<=array.length-2; i++){

for(let j=0; j<=array.length-1-i; j++){

if(array[j]<array[j+1]){
temp=array[j+1];
array[j+1]=array[j];
array[j]=temp
}

}

}
console.log(array)




The Time Complexity of Bubble Sort 


The time complexity of the bubble sort algorithm is high as the data size increases. bubble sort algorithm  is not suitable for large data size

the time complexity is O(n2)  2=square;
the space complexity is O(1)




Practical Question


sort the array in decreasing order  
array=[2, 5, 6, 23, 56, 3, 7, 8, 1]
Solution 




let array=[2, 5, 6, 23, 56, 3, 7, 8, 1];

for(let i=0; i<=array.length-2; i++){

for(let j=0; j<=array.length-1-i; j++){

if(array[j]<array[j+1]){
temp=array[j+1];
array[j+1]=array[j];
array[j]=temp
}

}

}
console.log(array)





Conclusion

Despite its simplicity, Bubble Sort embodies a profound concept: the power of incremental refinement. With each pass through the list, the algorithm iteratively improves the arrangement, akin to a craftsman meticulously honing their craft. This iterative refinement, while seemingly humble, is the cornerstone of Bubble Sort's efficacy.


Yet, Bubble Sort's simplicity belies its inefficiency when confronted with large datasets. Its time complexity of O(n^2) renders it impractical for sorting extensive lists, as the number of comparisons grows quadratically with the size of the input. However, in smaller datasets or educational contexts, its straightforward implementation serves as a pedagogical tool, illuminating core concepts of sorting algorithms.


In conclusion, Bubble Sort exemplifies elegance in simplicity. Its iterative approach, akin to the graceful ascent of bubbles, gradually transforms disorder into order through a series of meticulous exchanges. While not suited for large datasets, its intuitive nature, and incremental refinement make it a timeless illustration of fundamental sorting principles.




Post a Comment

Previous Post Next Post