Skip to content

Sorting Algorithms

Welcome to the Sorting Algorithms section — from the simplest O(n²) sorts to efficient O(n log n) algorithms. This module covers every major sorting algorithm with visual diagrams, JavaScript implementations, complexity analysis, and interview preparation.


StepTopicWhat You’ll Learn
1IntroductionWhat is sorting, stable vs unstable, in-place vs out-of-place
2Bubble SortSimplest sort, optimized with early exit
3Selection SortFind minimum repeatedly, place in position
4Insertion SortPlaying cards analogy, great for nearly sorted data
5Merge SortDivide & conquer, guaranteed O(n log n), stable
6Quick SortPivot-based partitioning, fast in practice
7Heap SortHeap data structure, O(n log n) with O(1) space
8Complexity ComparisonFull comparison table, when to use which
9Interview Questions15+ questions with detailed answers

AlgorithmBestAverageWorstSpaceStable
Bubble SortO(n)O(n²)O(n²)O(1)✅ Yes
Selection SortO(n²)O(n²)O(n²)O(1)❌ No
Insertion SortO(n)O(n²)O(n²)O(1)✅ Yes
Merge SortO(n log n)O(n log n)O(n log n)O(n)✅ Yes
Quick SortO(n log n)O(n log n)O(n²)O(log n)❌ No
Heap SortO(n log n)O(n log n)O(n log n)O(1)❌ No

Input Size?
├── Tiny (n < 20) ──────────────────────────────→ Insertion Sort
└── Larger
├── Nearly Sorted? ──────────────────────────→ Insertion Sort
├── Need Stable Sort?
│ ├── Memory Available ─────────────────────→ Merge Sort
│ └── Memory Constrained ──────────────────→ (no O(1) stable sort)
├── Need O(1) Space + O(n log n)?
│ └── Not stable needed ────────────────────→ Heap Sort
└── General Purpose
├── Average performance matters ──────────→ Quick Sort
└── Guaranteed performance needed ────────→ Merge Sort

These are easy to understand and implement but slow for large inputs:

  • Bubble Sort — repeatedly swap adjacent elements
  • Selection Sort — find minimum, place at front
  • Insertion Sort — build sorted portion one element at a time

Efficient / Logarithmic Sorts — O(n log n)

Section titled “Efficient / Logarithmic Sorts — O(n log n)”

Optimal comparison-based sorting algorithms:

  • Merge Sort — divide, sort halves, merge back
  • Quick Sort — partition around pivot recursively
  • Heap Sort — use a max-heap to extract in order

A sort is stable if equal elements maintain their original relative order.

Original: [(3,a), (1,b), (3,c), (2,d)]
↑ ↑
Both have key 3
Stable result: [(1,b), (2,d), (3,a), (3,c)] ← a before c ✅
Unstable result: [(1,b), (2,d), (3,c), (3,a)] ← c before a ❌
  • In-place: Sorts the array using O(1) extra space (Bubble, Selection, Insertion, Heap, Quick)
  • Out-of-place: Requires extra memory (Merge Sort uses O(n) extra space)
  • Comparison sorts: Compare elements to determine order (all 6 algorithms above) — lower bound is O(n log n)
  • Non-comparison sorts: Use element values directly (Counting Sort, Radix Sort, Bucket Sort) — can achieve O(n)

“What is the best sorting algorithm?” — There’s no single answer. It depends on input size, memory constraints, stability requirements, and data distribution.

“Which sort does JavaScript’s .sort() use?” — V8 uses TimSort (hybrid of merge sort + insertion sort) which is stable and O(n log n).

“Can you sort faster than O(n log n)?” — Yes, with non-comparison sorts like Counting Sort or Radix Sort, given constraints on the data range.


Start with Introduction to Sorting →