Bubble Sort is one of the simplest sorting algorithms taught in computer science. Its simplicity makes it easy to understand, implement, and visualize, but it is also one of the least efficient sorting methods. Despite its inefficiency, Bubble Sort is often used as an introductory sorting technique to teach fundamental algorithmic thinking.

In this article, we’ll cover:

  • What is Bubble Sort?
  • Step-by-step working of Bubble Sort
  • Dry run with examples
  • Python implementation of Bubble Sort
  • Time and space complexity analysis
  • Advantages and drawbacks
  • Comparison with other sorting algorithms

What is Bubble Sort?

Bubble Sort is a comparison-based sorting algorithm. It repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process continues until no more swaps are needed, which means the list is sorted.

The name “Bubble Sort” comes from the way smaller (or larger) elements β€œbubble” to the top of the list after each pass.

How Bubble Sort Works

The process can be understood in the following steps:

  1. Compare the first two adjacent elements.
  2. If the first is greater than the second, swap them.
  3. Move to the next pair, compare and swap if necessary.
  4. Repeat the process for the entire list. After the first pass, the largest element is at the end of the list.
  5. Repeat passes until the whole list is sorted.

Example of Bubble Sort (Dry Run)

Let’s take an example array: [5, 3, 8, 4, 2]

Pass 1:
[5, 3, 8, 4, 2] β†’ Compare 5 & 3 β†’ Swap β†’ [3, 5, 8, 4, 2]
[3, 5, 8, 4, 2] β†’ Compare 5 & 8 β†’ No Swap
[3, 5, 8, 4, 2] β†’ Compare 8 & 4 β†’ Swap β†’ [3, 5, 4, 8, 2]
[3, 5, 4, 8, 2] β†’ Compare 8 & 2 β†’ Swap β†’ [3, 5, 4, 2, 8]

Pass 2:
[3, 5, 4, 2, 8] β†’ Compare 3 & 5 β†’ No Swap
[3, 5, 4, 2, 8] β†’ Compare 5 & 4 β†’ Swap β†’ [3, 4, 5, 2, 8]
[3, 4, 5, 2, 8] β†’ Compare 5 & 2 β†’ Swap β†’ [3, 4, 2, 5, 8]

Pass 3:
[3, 4, 2, 5, 8] β†’ Compare 3 & 4 β†’ No Swap
[3, 4, 2, 5, 8] β†’ Compare 4 & 2 β†’ Swap β†’ [3, 2, 4, 5, 8]

Pass 4:
[3, 2, 4, 5, 8] β†’ Compare 3 & 2 β†’ Swap β†’ [2, 3, 4, 5, 8]

Final Sorted Array: [2, 3, 4, 5, 8]

Bubble Sort Algorithm: Simple but Inefficient Sorting Method Explained

Python Implementation of Bubble Sort


def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        # Flag to check if swap happened
        swapped = False
        for j in range(0, n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
                swapped = True
        if not swapped:
            break  # Stop early if no swaps occurred
    return arr

# Example Usage
print(bubble_sort([5, 3, 8, 4, 2]))

Interactive Visualization of Bubble Sort (Python)

You can simulate how Bubble Sort works step-by-step with the following snippet:


def bubble_sort_steps(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
            print(arr)  # Visual output at each step

bubble_sort_steps([5, 3, 8, 4, 2])

Complexity Analysis

Case Time Complexity
Best Case (Already Sorted) O(n)
Average Case O(n2)
Worst Case (Reverse Sorted) O(n2)

Space Complexity: O(1) since Bubble Sort sorts the array in place without needing additional memory.

Advantages of Bubble Sort

  • Very simple to understand and implement.
  • Works in-place (requires no extra storage).
  • Can be optimized to stop early if the list becomes sorted.

Disadvantages of Bubble Sort

  • Very slow for large datasets compared to other algorithms like Quick Sort or Merge Sort.
  • Has O(nΒ²) average complexity making it unfit for performance-critical applications.

Comparison with Other Sorting Algorithms

Here’s how Bubble Sort compares with some popular algorithms:

Algorithm Average Complexity Space Complexity
Bubble Sort O(nΒ²) O(1)
Selection Sort O(nΒ²) O(1)
Insertion Sort O(nΒ²) O(1)
Merge Sort O(n log n) O(n)
Quick Sort O(n log n) O(log n)

Conclusion

Bubble Sort is a basic sorting algorithm that is great for educational purposes but rarely used in practice due to its inefficiency. Understanding Bubble Sort, however, helps build a strong foundation for grasping more efficient sorting algorithms like Merge Sort and Quick Sort.

If you’re just starting out with algorithms, Bubble Sort is an excellent first step into the world of sorting and time complexity analysis.