Bubble Sort
The simplest sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. "Bubbling" the largest element to the top.
Bubble Sort Algorithm
Bubble Sort: Pass 0
Starting Bubble Sort with 7 elements
Current array: [64, 34, 25, 12, 22, 11, 90] (length: 7)
One pass of bubble sort
Order what happens during a single pass.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
- 1Drop a step here
- 2Drop a step here
- 3Drop a step here
- 4Drop a step here
- 5Drop a step here
Test your knowledge
What is the worst-case time complexity of Bubble Sort?
The short version
Imagine air bubbles in water. Heavier ones sink, lighter ones float up. In Bubble Sort, the largest element 'bubbles up' to the end of the list in each pass.
In more depth
How It Works
Bubble Sort is the "hello world" of sorting algorithms. It works by repeatedly stepping through the list, comparing adjacent elements, and swapping them if they are in the wrong order. The pass through the list is repeated until the list is sorted.
Key Concepts:
Why learn it?
While inefficient for large datasets (O(n²)), it introduces the concept of comparison-based sorting and swapping, which are foundational for more complex algorithms.
When to reach for this
- When the input is very small (e.g., N < 50)
- When the input is nearly sorted (efficient with early termination)
- When simplicity of implementation is the top priority
Interview Questions
References & Further Reading
Love this visualization?
Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.
100% free • No credit card required