Optimized Bubble Sort: Mechanism, Complexity Analysis & Interactive Visualizer
Optimized Bubble Sort
Bubble Sort with two small but valuable improvements: an early-exit flag and a shrinking last-swap boundary. The asymptotic class doesn't change, but real inputs run dramatically faster.
How it works
The plain Bubble Sort keeps doing passes even after the array is already sorted. Two optimizations fix that:
- Swapped flag. Before each pass, set
swapped = false. If you complete the pass without swapping anything, the array is sorted — break out immediately. - Last-swap boundary. The position of the last swap on a pass is the new upper bound; everything to the right is already sorted, so the next pass can stop there.
The best case becomes O(n) — one clean pass over an already-sorted input — while the worst case (reversed input) is still O(n²).
Complexity
Interactive visualization
Pseudocode
procedure optimizedBubbleSort(A): n ← length(A) repeat: lastSwap ← 0 for j from 0 to n − 2: if A[j] > A[j+1]: swap A[j], A[j+1] lastSwap ← j + 1 n ← lastSwap until n ≤ 1
JavaScript implementation
function optimizedBubbleSort(arr) { let n = arr.length; while (n > 1) { let lastSwap = 0; for (let j = 0; j < n - 1; j++) { if (arr[j] > arr[j + 1]) { [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; lastSwap = j + 1; } } n = lastSwap; // everything after the last swap is already sorted } return arr; }
Pros and cons
Pros
- Best case becomes
O(n)— one pass detects an already-sorted array. - Last-swap boundary shrinks the working range quickly on nearly-sorted input.
- Still stable and in-place.
- Two-line code change over plain Bubble Sort.
Cons
- Worst and average case remain
O(n²). - Insertion sort outperforms it in practice even when both are O(n) on sorted data.
- Adds two state variables — minor, but still moving parts.
When to use it
- Streams that arrive almost in order. Best case really is linear.
- Educational comparison with plain Bubble Sort, to demonstrate how a small flag changes the best-case curve.
- Embedded code-golf where every extra branch matters less than tiny code size.
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 Md. Harun-Or-Roshid
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 Md. Harun-Or-Roshid
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 Md. Harun-Or-Roshid
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 Md. Harun-Or-Roshid