Bubble Sort
Tags: dsa, python, codenewbie, beginners
Published: 2022-01-24 16:15:46

Hi Everyone, Hope you are safe.
I thought to create a series of sorting algorithms. I will try my best to make it simple. If you still have doubt let me know in the comment section.


Let's begin........

**Background:**
* Bubble sort is the simplest algorithm. This is used for understanding purpose of a student. How the sorting algorithms works.
* Bubble sort is good for small size of input.


**What is Bubble sort:**
Take the larger number to the end and repeatedly swapping the adjacent elements.

**Examples:**
arr = [4, 8, 1, 2, 6, 7]
**First times:**
* [4, 8, 1, 2, 6, 7] -> it compares between first and 2nd element i.e, 4>8. it is false. so, it didn't swap.
* [4, 8, 1, 2, 6, 7] -> it compares between 2nd and 3rd element i.e 8>1. Now array is [4, **1**, **8**, 2, 6, 7].same way it will go for others array values.
* [4, 1, 8, 2, 6, 7] -> [4, 1, **2**, **8**, 6, 7], 8>2,swapped it.
* [4, 1, 2, 8, 6, 7] -> [4, 1, 2, **6**, **8**, 7], 8>6,swapped it.
* [4, 1, 2, 6, 8, 7] -> [4, 1, 2, 6, **7**, **8**], 8>7,swapped it.

**Second times:**
* [4, 1, 2, 6, 7, 8] -> [**1**, **4**, 2, 6, 7, 8], 4>1,swapped it.
* [1, 4, 2, 6, 7, 8] -> [1, **2**, **4**, 6, 7, 8], 4>2,swapped it.
* [1, 2, 4, 6, 7, 8] -> [1, 2, 4, 6, 7, 8], elements(4 & 6) are already in correct position so it did not swap.
* [1, 2, 4, 6, 7, 8] -> [1, 2, 4, 6, 7, 8], elements(6 & 7) are already in correct position so it did not swap.

It will go for other iterations. Check the below illustration for better understanding.

![bubble sort explanation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/siqy1unnhdjv3mij3jn9.jpg)

- We can see that larger element is going at end position using this algorithm.

**Let's implement so far we discussed:**

![bubble sort code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6anu0jz0sjxt3t0wqimh.png)


**output:**
![output of bubble sort](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4krto2t2jkpn3p8xdkhf.png)


**Time complexity:**
The time complexity of this algorithm is **O(n^2)** for worst case.

when _times=0_ we iterate till _n-1_ where n = len(arr). 
when _times=1_ we iterate till _n-2_ where n = len(arr).
when _times=2_ we iterate till _n-3_ where n = len(arr).
and so no.........

Based on above iteration, we calculated the time complexity.

![time complexity](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kg7vm6332k444m14vnfk.png)

**Optimization:** As per the above code, time complexity would be O(n^2) even though array is in sorted order. Because the inner loop always iterate and swap the value. If we stop the swapping then the time complexity would be O(n) for sorted array.

![optimized code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vnu366rocba6fqoftmnf.png)

**Output:**
![optimized output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wg2m0fbkzwjzcd190y71.png)
 

**Worst and Average Case Time Complexity:** **O(n^2)**. worst case occurs when array is in descending order.
**Best Case Time Complexity:** O(n) when array is in sorted order.
**Space Complexity:** There is no extra array or something else so space complexity should be **O(1)**.
[Stable](https://www.geeksforgeeks.org/stability-in-sorting-algorithms/#:~:text=A%20sorting%20algorithm%20is%20said,input%20array%20to%20be%20sorted): Yes