Oh My Algorithm
Algorithm Guidecomplexity: O(n²)

Bubble Sort

Sorts by comparing adjacent elements; the larger value is pushed one slot at a time toward the end, which is where the name comes from. Simple to implement, but unsuitable for large data.

01Bubble Sort

Starting bubble sort. Compare each neighbouring pair and push the larger one toward the back, over and over.

Compare the first element 45 with the second element 12.

45 > 12, so swap them. The larger value bubbles toward the end.

Compare 45 and 89. 89 is larger, so keep them as is.

Compare 89 and 34, then swap.

Compare 89 and 67, then swap.

Compare 89 and 23, then swap.

Compare 89 and 56, then swap.

Finally, compare 89 and 10, then swap.

First pass complete! The largest value 89 has settled into the last position.

Complete the remaining passes (2–7). Each pass bubbles the largest value toward the end.

Bubble sort complete! [10, 12, 23, 34, 45, 56, 67, 89] — every element is sorted in ascending order.

45
12
89
34
67
23
56
10
1 / 12

02 Understand It Simply

For Everyone
🔑How It Works

Compares neighboring values and swaps them when they are out of order. Each pass settles the largest remaining value at the end.

💡In Plain Words

Repeatedly compares neighbors and swaps them when they're out of order.

Simple but slow (O(n²)), so it's a poor fit for large data.

📍Where It's Used
  • Learning sorting concepts
  • small nearly-sorted data

03 Python Implementation

A clean, readable reference implementation of the core logic of Bubble Sort.

core_implementation.py
def bubble_sort(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]
    return arr

04 Frequently Asked Questions

FAQ
What is Bubble Sort?+

Sorts by comparing adjacent elements; the larger value is pushed one slot at a time toward the end, which is where the name comes from. Simple to implement, but unsuitable for large data.

What is the time complexity of Bubble Sort?+

The time complexity of Bubble Sort is O(n²). Follow the step-by-step visualization to see exactly why.

Where is Bubble Sort used?+

Learning sorting concepts, small nearly-sorted data.

What's a simple analogy for Bubble Sort?+

Compares neighboring values and swaps them when they are out of order. Each pass settles the largest remaining value at the end.