072
LVL 03 — MID DEVELOPERSESSION 072DAY 72

SORTING ALGORITHMS

🎫 PIXELCRAFT-059
Performance | 🟡 Medium | Priority: 🟡 Medium

The client-side gallery sort is noticeably slow on large collections. Currently using a naive bubble sort. Implement efficient sorting and build a visualization.
CONCEPTS.UNLOCKED
🫧
Bubble Sort
O(n²) — simple but slow. Compare adjacent elements, swap if out of order, repeat. Easy to understand, terrible for large datasets. The "Hello World" of sorting.
🔀
Merge Sort
O(n log n) — divide and conquer, stable, predictable. Split in half, sort each half recursively, merge them back. Guaranteed performance regardless of input order.
Quick Sort
O(n log n) average — fast in practice. Pick a pivot, partition around it, sort the partitions. Used by most language runtimes. Worst case O(n²) but rare with good pivot selection.
🔧
JavaScript's .sort()
TimSort — hybrid merge + insertion sort. O(n log n), stable, optimized for real-world data. V8's implementation since 2019. You don't need to beat it — understand it.
⚖️
Sorting Stability
Does the order of equal elements get preserved? Merge sort: stable. Quick sort: unstable. Matters when sorting by multiple fields — sort by name, then by date.
🎛️
Custom Comparators
Multi-field sorting with compare functions. Sort images by date descending, then by name ascending for same dates. The comparator defines the ordering logic.
HANDS-ON.TASKS
01
Implement Bubble Sort
function bubbleSort(arr, compareFn) { const a = [...arr]; for (let i = 0; i < a.length; i++) { for (let j = 0; j < a.length - i - 1; j++) { if (compareFn(a[j], a[j+1]) > 0) { [a[j], a[j+1]] = [a[j+1], a[j]]; } } } return a; }
02
Implement Merge Sort
function mergeSort(arr, compareFn) { if (arr.length <= 1) return arr; const mid = Math.floor(arr.length / 2); const left = mergeSort( arr.slice(0, mid), compareFn ); const right = mergeSort( arr.slice(mid), compareFn ); return merge(left, right, compareFn); } function merge(left, right, compareFn) { const result = []; let i = 0, j = 0; while (i < left.length && j < right.length) { if (compareFn( left[i], right[j]) <= 0) { result.push(left[i++]); } else { result.push(right[j++]); } } return [ ...result, ...left.slice(i), ...right.slice(j) ]; }
03
Benchmark the Difference
Algorithm10,000 itemsComplexity
Bubble Sort~2,800msO(n²)
Merge Sort~35msO(n log n)
JS .sort()~28msO(n log n)
80× faster. For 1,000,000 items the gap is 50,000×. This is why algorithm choice matters — the difference isn't linear, it's exponential.
04
Build a Sorting Visualizer

Build a <SortingVisualizer> React component: colorful bars that swap and merge in real-time. Toggle between bubble sort and merge sort. Watch the algorithms work — see WHY one is faster.

05
Apply to PixelCraft Gallery

Replace bubble sort with multi-field sorting: sort by date descending, then by name ascending for same dates. Use JavaScript's built-in .sort() with a custom comparator.

06
Close the Ticket
git switch -c feature/PIXELCRAFT-059-sorting git add src/ git commit -m "Replace O(n²) sort with efficient sorting + visualizer (PIXELCRAFT-059)" git push origin feature/PIXELCRAFT-059-sorting # PR → Review → Merge → Close ticket ✅
CS.DEEP-DIVE

Sorting analysis is the canonical CS problem.

Understanding WHY merge sort is O(n log n) — not just memorizing it — teaches you to analyze ANY algorithm.

// Why is merge sort O(n log n)?

log n levels of recursion
  (halving each time)

Each level does O(n) work
  (merging all elements)

n × log n = n log n

// For 10,000 items:
Bubble sort: ~100,000,000 comparisons
Merge sort:  ~130,000 comparisons
That's 770× fewer.

// For 1,000,000 items:
Bubble sort: ~1,000,000,000,000
Merge sort:  ~20,000,000
That's 50,000× fewer.

// Mathematical reasoning, not guessing.
"Sorting Lab"
[A]Add quick sort to the visualizer. Implement Lomuto partition scheme. Compare all three visually: bubble sort (swapping everywhere), merge sort (splitting and merging), quick sort (partitioning around pivot).
[B]Benchmark all three algorithms at different scales: 100, 1000, 10,000, 100,000 items. Plot the results in a chart. See the O(n²) curve diverge from O(n log n) dramatically.
[C]Research: why does V8 use TimSort instead of pure merge sort or quick sort? What is "natural order" in real-world data, and how does TimSort exploit it? Write a brief analysis.
REF.MATERIAL
VIDEO
Timo Bingmann
Mesmerizing visualization of sorting algorithms with sound. See and hear the difference between O(n²) and O(n log n). The most watched sorting video on the internet.
SORTINGVISUALESSENTIAL
VIDEO
Harvard CS50
David Malan's explanation of merge sort: the divide-and-conquer intuition, why it's O(n log n), and the beautiful recursion behind it.
MERGE SORTCS
ARTICLE
V8 Team
Why V8 switched from QuickSort to TimSort for Array.prototype.sort(). Stability, performance on real-world data, and the engineering tradeoffs.
TIMSORTV8OFFICIAL
ARTICLE
Toptal
Interactive sorting visualizations: compare algorithms on random, nearly sorted, reversed, and few-unique datasets. See how input shape affects performance.
SORTINGINTERACTIVE
ARTICLE
Wikipedia
Comprehensive comparison of sorting algorithms: time complexity, space complexity, stability, and the theoretical lower bound of O(n log n).
SORTINGTHEORYCS
// LEAVE EXCITED BECAUSE
You watched merge sort beat bubble sort by 80× on real data. The sorting visualizer makes the algorithm visible. You understand WHY code is fast or slow — from mathematical reasoning, not guessing.