## DAA Bubble SortBubble Sort, also known as Exchange Sort, is a simple sorting algorithm. It works by repeatedly stepping throughout the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is duplicated until no swaps are desired, which means the list is sorted. This is the easiest method among all sorting algorithms. ## Algorithm
## How Bubble Sort Works- The bubble sort starts with the very first index and makes it a bubble element. Then it compares the bubble element, which is currently our first index element, with the next element. If the bubble element is greater and the second element is smaller, then both of them will swap.
After swapping, the second element will become the bubble element. Now we will compare the second element with the third as we did in the earlier step and swap them if required. The same process is followed until the last element. - We will follow the same process for the rest of the iterations. After each of the iteration, we will notice that the largest element present in the unsorted array has reached the last index.
For each iteration, the bubble sort will compare up to the last unsorted element. Once all the elements get sorted in the ascending order, the algorithm will get terminated. Consider the following example of an unsorted array that we will sort with the help of the Bubble Sort algorithm.
**Compare a**_{0}and a_{1}
As a **Compare a**_{1}and a_{2}
Now a **Compare a**_{2}and a_{3}
As a **Compare a**_{3}and a_{4}
Here a
**Compare a**_{0}and a_{1}
As a **Compare a**_{1}and a_{2}
Here a **Compare a**_{2}and a_{3}
In this case, a
**Compare a**_{0}and a_{1}
As a **Compare a**_{1}and a_{2}
Now a
**Compare a**_{0}and a_{1}
Here a Hence the array is sorted as no more swapping is required. ## Complexity Analysis of Bubble Sort
Therefore, the bubble sort algorithm encompasses a time complexity of O(1) because it necessitates some extra memory space for temp variable for swapping.## Time Complexities:**Best Case Complexity**: The bubble sort algorithm has a best-case time complexity of**O(n)**for the already sorted array.**Average Case Complexity**: The average-case time complexity for the bubble sort algorithm is**O(n**, which happens when 2 or more elements are in jumbled, i.e., neither in the ascending order nor in the descending order.^{2})**Worst Case Complexity**: The worst-case time complexity is also**O(n**, which occurs when we sort the descending order of an array into the ascending order.^{2})
## Advantages of Bubble Sort- Easily understandable.
- Does not necessitates any extra memory.
- The code can be written easily for this algorithm.
- Minimal space requirement than that of other sorting algorithms.
## Disadvantages of Bubble Sort- It does not work well when we have large unsorted lists, and it necessitates more resources that end up taking so much of time.
- It is only meant for academic purposes, not for practical implementations.
- It involves the n
^{2}order of steps to sort an algorithm.
Next TopicSelection Sort |