Skip to main content

๐Ÿ”ƒ Sorting

One-line summary: Rearrange elements in order โ€” the prerequisite for binary search, two pointers, and many greedy algorithms.


Enhanced Visualizationโ€‹

Enhanced Sorting Algorithm Comparison Interactive comparison of Bubble Sort, Quick Sort, and Merge Sort with real-time performance metrics

Sorting Flow Sorting GIF

Algorithm Analysisโ€‹

The enhanced animation above demonstrates three fundamental sorting paradigms:

๐Ÿ”ด Bubble Sort - Simple but Inefficientโ€‹

  • Strategy: Compare adjacent elements and swap if out of order
  • Performance: ~500,000 operations for n=1000 elements
  • Visual: Watch elements "bubble" to their correct positions
  • Best for: Educational purposes, very small datasets

๐ŸŸฃ Quick Sort - Efficient Divide-and-Conquerโ€‹

  • Strategy: Choose pivot, partition around it, recursively sort
  • Performance: ~10,000 operations for n=1000 elements
  • Visual: See pivot selection and partitioning phases
  • Best for: General-purpose sorting, in-place requirements

๐Ÿ”ต Merge Sort - Stable and Predictableโ€‹

  • Strategy: Divide array, sort halves, merge sorted results
  • Performance: ~10,000 operations for n=1000 elements
  • Visual: Observe divide-and-conquer with merging phases
  • Best for: Stable sorting, linked lists, external sorting

Complete Algorithm Comparisonโ€‹

AlgorithmTime (best)Time (avg)Time (worst)SpaceStable?When to Use
Bubble SortO(n)O(nยฒ)O(nยฒ)O(1)YesTeaching only; nearly-sorted tiny arrays
Selection SortO(nยฒ)O(nยฒ)O(nยฒ)O(1)NoMinimizing number of swaps
Insertion SortO(n)O(nยฒ)O(nยฒ)O(1)YesSmall or nearly-sorted data; online sorting
Merge SortO(n log n)O(n log n)O(n log n)O(n)YesStable sort; linked lists; external sorting
Quick SortO(n log n)O(n log n)O(nยฒ)O(log n)NoGeneral-purpose, in-place, cache-friendly
Heap SortO(n log n)O(n log n)O(n log n)O(1)NoGuaranteed O(n log n) with O(1) space
Counting SortO(n + k)O(n + k)O(n + k)O(k)YesSmall integer range k
Radix SortO(nk)O(nk)O(nk)O(n+k)YesFixed-width integers/strings

k = range of input values (counting) or number of digits (radix). Stable = equal elements keep their original relative order.

Cyclic Sort: for arrays containing numbers in range [1..n] โ€” place each number at index num-1 in O(n), O(1) space.

๐Ÿงญ Which Sort Should I Use?โ€‹

flowchart TD
A["Need to sort?"] --> B{"Small input\n(n < 50) or\nnearly sorted?"}
B -->|Yes| C["Insertion Sort"]
B -->|No| D{"Values are\nsmall integers?"}
D -->|Yes| E["Counting / Radix Sort\nO(n+k)"]
D -->|No| F{"Need stability?"}
F -->|Yes| G["Merge Sort\nO(n log n), O(n)"]
F -->|No| H{"Memory constrained?"}
H -->|Yes| I["Heap Sort\nO(n log n), O(1)"]
H -->|No| J["Quick Sort\navg O(n log n), in-place"]

Stability โ€” Why It Mattersโ€‹

A stable sort preserves the relative order of records with equal keys. This is essential when sorting by multiple criteria (e.g., sort by name, then stably by age). Merge, insertion, counting, and radix sorts are stable; quick, heap, and selection sorts are not.


Common Patternsโ€‹

Merge Sortโ€‹

function mergeSort(arr) {
if (arr.length <= 1) return arr;
const mid = Math.floor(arr.length / 2);
return merge(mergeSort(arr.slice(0, mid)), mergeSort(arr.slice(mid)));
}
function merge(left, right) {
const result = [];
let i = 0,
j = 0;
while (i < left.length && j < right.length)
result.push(left[i] <= right[j] ? left[i++] : right[j++]);
return result.concat(left.slice(i), right.slice(j));
}

Cyclic Sortโ€‹

function cyclicSort(nums) {
let i = 0;
while (i < nums.length) {
const correct = nums[i] - 1;
if (nums[i] !== nums[correct]) [nums[i], nums[correct]] = [nums[correct], nums[i]];
else i++;
}
return nums;
}

Quick Sort (in-place, Lomuto partition)โ€‹

function quickSort(arr, lo = 0, hi = arr.length - 1) {
if (lo >= hi) return arr;
const pivot = arr[hi];
let i = lo;
for (let j = lo; j < hi; j++) {
if (arr[j] < pivot) {
[arr[i], arr[j]] = [arr[j], arr[i]];
i++;
}
}
[arr[i], arr[hi]] = [arr[hi], arr[i]]; // place pivot
quickSort(arr, lo, i - 1);
quickSort(arr, i + 1, hi);
return arr;
}
// Avg O(n log n); randomize the pivot to avoid the O(n^2) worst case.

Insertion Sort (great for small / nearly-sorted arrays)โ€‹

function insertionSort(arr) {
for (let i = 1; i < arr.length; i++) {
const key = arr[i];
let j = i - 1;
while (j >= 0 && arr[j] > key) arr[j + 1] = arr[j--];
arr[j + 1] = key;
}
return arr;
}
// O(n) on nearly-sorted input, O(n^2) worst case. Stable and in-place.

๐Ÿ“š Implementations: Full, tested implementations of each algorithm live alongside this guide under concepts/sorting/. Study the comparison table above, then read the code for the algorithm you need.


Pitfallsโ€‹

  • Quick sort worst case O(nยฒ) on sorted input โ€” use random pivot or 3-way partition
  • Stable vs unstable: use stable sort when equal elements must maintain relative order
  • JS Array.sort() is not guaranteed stable in older engines (ES2019+ guarantees it)

Practice Problemsโ€‹

ProblemDifficultySolution
LC 75 โ€” Sort ColorsMedium
LC 912 โ€” Sort an ArrayMedium
LC 148 โ€” Sort ListMedium
LC 179 โ€” Largest NumberMedium
LC 315 โ€” Count of Smaller Numbers After SelfHard
CC โ€” Turbo Sort (TSORT)Easy
CC โ€” Inversion Count (INVCNT)Medium
CC โ€” Smallest Difference (SMASTR)Easy
LC 164 โ€” Maximum GapHard
LC 905 โ€” Sort Array By ParityEasy
LC 1636 โ€” Sort Array by Increasing FrequencyEasy
LC 56 โ€” Merge IntervalsMedium
LC 57 โ€” Insert IntervalMedium
LC 147 โ€” Insertion Sort ListMedium
LC 215 โ€” Kth Largest Element in an ArrayMedium
LC 242 โ€” Valid AnagramEasy
LC 252 โ€” Meeting RoomsEasy
LC 253 โ€” Meeting Rooms IIMedium
LC 274 โ€” H-IndexMedium
LC 324 โ€” Wiggle Sort IIMedium
LC 347 โ€” Top K Frequent ElementsMedium
LC 349 โ€” Intersection of Two ArraysEasy
LC 350 โ€” Intersection of Two Arrays IIEasy
LC 435 โ€” Non-overlapping IntervalsMedium
LC 451 โ€” Sort Characters By FrequencyMedium
LC 506 โ€” Relative RanksEasy
LC 561 โ€” Array PartitionEasy
LC 692 โ€” Top K Frequent WordsMedium
LC 791 โ€” Custom Sort StringMedium
LC 853 โ€” Car FleetMedium
LC 922 โ€” Sort Array By Parity IIEasy
LC 973 โ€” K Closest Points to OriginMedium
LC 1122 โ€” Relative Sort ArrayEasy
LC 1365 โ€” How Many Numbers Are Smaller Than CurrentEasy
CC โ€” Merge Sort (MERGSORT)Medium
CC โ€” Quick Sort (QUICKSRT)Medium
CC โ€” Counting Sort (CNTSORT)Easy

  • Binary Search โ€” requires sorted input
  • Two Pointers โ€” converging pointers need sorted array
  • Heap โ€” heapsort; partial sort for Top-K

โ† Back to Home ยท ยฉ sparshjaswal