Bubble Sort: Mechanism, Complexity Analysis & Interactive Visualizer
Bubble Sort
The classic teaching algorithm: repeatedly walk the array, swap any out-of-order neighbors, and let the largest element bubble to the top of each pass.
How it works
Bubble Sort makes repeated left-to-right passes over the array. On each pass it compares every adjacent pair of elements and swaps them if they are in the wrong order. Each full pass guarantees that one more of the largest unsorted values has reached its final position at the end of the array, like a bubble of air rising to the surface of water.
- Start at index
0. - Compare
arr[i]witharr[i+1]. - If they are out of order, swap them.
- Move one step right and repeat until you reach the end of the unsorted portion.
- After each pass, the last position of the unsorted portion is fixed; shrink the working range and repeat until no swaps are possible.
Complexity
Interactive visualization
Pseudocode
procedure bubbleSort(A): n ← length(A) for i from 0 to n − 2: for j from 0 to n − i − 2: if A[j] > A[j+1]: swap A[j], A[j+1]
JavaScript implementation
function bubbleSort(arr) { const n = arr.length; for (let i = 0; i < n - 1; i++) { for (let j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; } } } return arr; }
Pros and cons
Pros
- Trivially simple to understand and implement.
- Stable: equal keys keep their original order.
- In-place: needs only
O(1)extra memory. - Detects "already sorted" trivially when paired with an early-exit flag.
Cons
- Quadratic in best, average, and worst case — useless beyond a few hundred elements.
- Performs many redundant swaps compared to selection sort.
- Cache-friendly but still slower than insertion sort in practice.
- Almost never used in production. Mainly a teaching tool.
When (not) to use it
- Teach the idea of comparison-based sorting. It is the canonical first algorithm.
- Tiny arrays (n < 10). Performance differences disappear, and the simplicity is a feature.
- Never use it for real workloads. Insertion sort dominates it on small inputs, and any O(n log n) sort dominates it on large ones.
Related reading
Sleep Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A unique concurrency-based sorting algorithm: spawns a separate thread for each element, sleeping for a duration proportional to the element value before writing it to the output.
By Super Admin
Cube Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A parallel dimension sorting algorithm: organizes elements in a 3D coordinate cube and sorts vectors along the height, width, and depth axes sequentially to achieve sorted order.
By Super Admin
Patience Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A card-game inspired sorting algorithm: distributes elements onto a series of piles such that each pile's top is \(\ge\) the incoming value, then performs a multi-way merge across pile tops.
By Super Admin
Library Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A gapped variation of Insertion Sort: leaves empty slots (gaps) in the array, allowing elements to be inserted in \(O(1)\) shift time on average by avoiding massive shifts.
By Super Admin