Bubble sort in 2 minutes
Channel: Michael Sambol
Full video link:
https://www.youtube.com/watch?v=xli_FI7CuzAAlgorithm • medium
Bubble sort repeatedly compares adjacent elements and swaps out-of-order pairs until the list is sorted.
It is mainly valuable as a teaching algorithm because its mechanics are intuitive and visual.
At scale, bubble sort is usually replaced by faster O(n log n) algorithms.
Typical Complexity Baseline
| Metric | Value |
|---|---|
| Complexity Note 1 | Average/Worst O(n^2) |
| Best | O(n) with early-exit optimization |
Prefer video learning? This explainer gives a quick visual walkthrough of the core idea before you dive into the detailed sections below.
Bubble sort in 2 minutes
Channel: Michael Sambol
Full video link:
https://www.youtube.com/watch?v=xli_FI7CuzAStep through Bubble Sort to see how the list changes over time.
Start with unsorted values.
Learn the core building blocks and terminology in one place before comparisons, so the mechanics are clear and duplicates are removed.
Outer pass
What it is: Each pass bubbles the largest remaining element to the end.
Why it matters: Outer pass is a required building block for understanding how Bubble Sort stays correct and performant on large inputs.
Adjacent comparison
What it is: Compare index i and i+1, then swap if needed.
Why it matters: Adjacent comparison is a required building block for understanding how Bubble Sort stays correct and performant on large inputs.
Swap operation
What it is: In-place exchange of adjacent values.
Why it matters: Swap operation is a required building block for understanding how Bubble Sort stays correct and performant on large inputs.
Early-exit flag
What it is: Stop when a full pass makes zero swaps.
Why it matters: Early-exit flag is a required building block for understanding how Bubble Sort stays correct and performant on large inputs.
Shrinking boundary
What it is: Ignore already sorted tail section in later passes.
Why it matters: Shrinking boundary is a required building block for understanding how Bubble Sort stays correct and performant on large inputs.
Adjacent swap
What it is: Swap two neighboring values only.
Why it matters: Swap two neighboring values only. In Bubble Sort, this definition helps you reason about correctness and complexity when inputs scale.
Stable sort
What it is: Equal values preserve relative order.
Why it matters: Equal values preserve relative order. In Bubble Sort, this definition helps you reason about correctness and complexity when inputs scale.
Best case
What it is: With early-exit, sorted input can finish in O(n).
Why it matters: With early-exit, sorted input can finish in O(n). In Bubble Sort, this definition helps you reason about correctness and complexity when inputs scale.
Worst case
What it is: Reverse-sorted input needs O(n^2) comparisons/swaps.
Why it matters: Reverse-sorted input needs O(n^2) comparisons/swaps. In Bubble Sort, this definition helps you reason about correctness and complexity when inputs scale.
In-place
What it is: Uses constant extra memory beyond input array.
Why it matters: Uses constant extra memory beyond input array. In Bubble Sort, this definition helps you reason about correctness and complexity when inputs scale.
Follow one Bubble Sort run end-to-end: compare neighbors, swap when needed, and stop early when no swaps occur.
Step 1
Each pass bubbles the largest remaining element to the end.
Why this step matters: The sorted suffix grows one element per pass, reducing work in subsequent passes.
Step 2
Compare index i and i+1, then swap if needed.
Why this step matters: Correct local comparisons are the core decision point that drives every swap in bubble sort.
Step 3
In-place exchange of adjacent values.
Why this step matters: Swapping only out-of-order neighbors preserves stability while moving larger values rightward.
Step 4
Stop when a full pass makes zero swaps.
Why this step matters: Early exit avoids unnecessary passes and gives near-sorted inputs a much faster best-case runtime.
vs Merge Sort
When to choose this: Choose bubble sort only for learning or tiny datasets.
Tradeoff: Merge sort is much faster for non-trivial input sizes.
vs Quick Sort
When to choose this: Bubble is simpler to explain line-by-line.
Tradeoff: Quick sort is far better in practical runtime.
vs Insertion Sort
When to choose this: Bubble may be easier to visualize in classrooms.
Tradeoff: Insertion sort usually performs fewer writes on near-sorted data.
CS education platforms
Learning environments use bubble sort visualizations to teach comparisons and swaps before advanced algorithms.
Takeaway: Bubble sort is a strong pedagogy tool, not a production workhorse.
Embedded prototypes
Tiny fixed-size arrays in constrained firmware sometimes use simple O(n^2) routines for readability.
Takeaway: For very small n, code simplicity can outweigh asymptotic optimality.
Interview prep companies
Candidate training often starts with bubble sort to build intuition for loop invariants and boundary handling.
Takeaway: Foundational understanding improves mastery of faster sorts later.
Stop once a pass makes no swaps, indicating the list is already sorted.
Complexity: Average/Worst O(n^2), Best O(n), Space O(1)
Bubble sort with early-exit optimization
function bubbleSort(values: number[]): number[] {
const sortedValues = [...values]
for (let passIndex = 0; passIndex < sortedValues.length - 1; passIndex += 1) {
let didSwap = false
for (let compareIndex = 0; compareIndex < sortedValues.length - 1 - passIndex; compareIndex += 1) {
if (sortedValues[compareIndex] > sortedValues[compareIndex + 1]) {
;[sortedValues[compareIndex], sortedValues[compareIndex + 1]] = [sortedValues[compareIndex + 1], sortedValues[compareIndex]]
didSwap = true
}
}
if (!didSwap) break
}
return sortedValues
}
O(n) behavior without early-exit optimization.Use these signals to decide if this data structure/algorithm is the right fit before implementation.
| # | Problem | Difficulty | Typical Complexity |
|---|---|---|---|
| 1 | Valid Anagram | Easy | O(n log n) with sorting |
| 2 | Merge Sorted Array | Easy | O(n+m) |
| 3 | Height Checker | Easy | O(n log n) |
| 4 | Sort an Array | Medium | O(n^2) for bubble approach |
| 5 | Sort Colors | Medium | O(n) optimal, O(n^2) naive bubble |
| 6 | Largest Number | Medium | O(n log n) |
| 7 | Wiggle Sort II | Medium | O(n log n) |
| 8 | Sort List | Medium | O(n log n) |
| 9 | Count of Smaller Numbers After Self | Hard | O(n log n) |
| 10 | Reverse Pairs | Hard | O(n log n) |