Sorting Algorithms

1 min read
Foundational1 min read
Rapid overview

Sorting Algorithms

TL;DR

Use this sheet to explain trade-offs quickly and reference JavaScript examples.

How it works


Common algorithms

AlgorithmAvg TimeWorst TimeSpaceStableNotes
Quick sortO(n log n)O(n^2)O(log n)Fast average, not stable
Merge sortO(n log n)O(n log n)O(n)Stable, good for large arrays
Heap sortO(n log n)O(n log n)O(1)In-place, not stable

JavaScript notes

  • Array.prototype.sort is stable in modern engines.
  • Always provide a comparator for numeric sorting.
numbers.sort((a, b) => a - b);

Interview prompt

  • Why is comparator-based sorting safer than default sort?

See also