Bubble sort is a simple comparison-based sorting algorithm that repeatedly steps through a list or array, compares adjacent elements, and swaps them if they are in the wrong order.
Here’s a step-by-step explanation of Bubble Sort:
- Initialization: Start by considering the entire list unsorted.
- Passes through the list: Bubble sort performs multiple passes through the list. In each pass, it compares adjacent pairs of elements and may swap them if they are in the wrong order.
- Comparing and Swapping: During each pass, bubble sort compares two adjacent elements. If the element on the left is greater than the element on the right (for sorting in ascending order), it swaps them. If they are already in the correct order, no swap is performed.
- Pass Completion: After one pass through the list, the largest (or smallest, depending on the sorting order) element will have “bubbled up” to its correct position at the end of the list. This means that the last element is now in its final sorted position.
- Repeat Passes: Continue this process for the remaining elements (excluding the last one, which is already in its correct position). In each subsequent pass, the next largest (or smallest) element will bubble up to its correct position.
- Termination: The sorting process continues until no more swaps are needed during a pass. At this point, the entire list is sorted.
Bubble sort is a simple sorting algorithm that is suitable for very specific scenarios where its simplicity and ease of implementation outweigh its inefficiency.
Understanding Bubble Sort with an example
Bubble sort repeatedly compares and swaps adjacent elements until no more swaps are needed. In each pass, the largest unsorted element “bubbles up” to its correct position.
Example-1 Unsorted List: [23, 45, 12, 89, 43]
Step 1:
- Compare 23 and 45. Since 23 is smaller, no swap is needed.
- Compare 45 and 12. Swap them.
- Compare 45 and 89. Since 45 is smaller, no swap is needed.
- Compare 89 and 43. Swap them.
Step 2:
- Compare 23 and 12. Swap them.
- Compare 23 and 45. Since 23 is smaller, no swap is needed.
- Compare 45 and 43. Swap them.
Step 3:
- Compare 12 and 23. Since 12 is smaller, no swap is needed.
- Compare 23 and 43. Since 23 is smaller, no swap is needed.
Step 4:
- Compare 12 and 23. Since 12 is smaller, no swap is needed.
Step 5 (Final Step):
- No more swaps needed.
Now, the entire list is sorted in ascending order:
Sorted List: [12, 23, 43, 45, 89]
Example-2 Unsorted List: [27, 5, 2, 80, 44, 67]
Step 1:
- Compare 27 and 5. Swap them.
- Compare 27 and 2. Swap them.
- Compare 27 and 80. Since 27 is smaller, no swap is needed.
- Compare 80 and 44. Swap them.
- Compare 80 and 67. Swap them.
Step 2:
- Compare 5 and 2. Swap them.
- Compare 5 and 27. Since 5 is smaller, no swap is needed.
- Compare 27 and 44. Since 27 is smaller, no swap is needed.
Step 3:
- Compare 2 and 5. Since 2 is smaller, no swap is needed.
- Compare 5 and 27. Since 5 is smaller, no swap is needed.
Step 4:
- No more swaps needed.
Now, the entire list is sorted in ascending order:
Sorted List: [2, 5, 27, 44, 67, 80]
Python implementation of Bubble Sort

Bubble sort has a time complexity of O(n^2), which makes it highly inefficient for sorting large datasets. For practical applications, especially when sorting large or partially sorted lists, more efficient sorting algorithms like quicksort, merge sort, or even insertion sort are recommended.
Some situations in which bubble sort might be considered:
- Small Data Sets: Bubble sort can be used when dealing with very small lists or arrays where the number of elements is extremely limited. In such cases, the overhead of implementing more complex sorting algorithms may not be justified, and the simplicity of bubble sort can be an advantage.
- Simple Sorting Requirements: If you have a simple sorting requirement, such as sorting a list of a few items for a one-time task, bubble sort’s ease of implementation may be sufficient. However, keep in mind that even for small lists, other algorithms like insertion sort or selection sort are usually more efficient.
- Stable Sorting: If you require a stable sorting algorithm (one that preserves the relative order of equal elements), bubble sort can be used. It is inherently stable, meaning it won’t change the order of equal elements during the sorting process.
In summary, bubble sort is rarely used in production code due to its inefficiency, but it still has value in educational contexts, for sorting very small datasets, or when simplicity and stability are more important than sorting speed.