Skip to content

Sorting algorithms ​

Sorting puts elements in order. It looks simple, but different algorithms make very different trade-offs between speed, memory, stability and simplicity — which is why every course on algorithms starts here.

Comparison ​

AlgorithmBestAverageWorstMemoryStableIn place
Bubblenn²n²1yesyes
Selectionn²n²n²1noyes
Insertionnn²n²1yesyes
Shelln log n≈ n^1.25n^1.51noyes
Mergenn log nn log nnyesno
Quickn log nn log nn²log nnoyes
Heapn log nn log nn log n1noyes
Countingn + kn + kn + kn + kyesno
Radixd(n + b)d(n + b)d(n + b)n + byesno

All complexities are O(…). k is the range of values, d the number of digits, b the base (10).

Key ideas ​

Comparison sorts (bubble … heap) only ask "is a less than b?". Any such algorithm needs at least log2⁡n!≈nlog2⁡n comparisons in the worst case: there are n! possible orders, and each comparison at best halves them. So O(n log n) is the best possible — merge sort and heap sort reach it always, quick sort on average.

Counting and radix sort are not comparison sorts: they use the values themselves as array indexes. That is how they beat n log n — but they only work for integers (or keys that can be turned into integers).

Stability — equal elements keep their original order. It matters when you sort records by one key after another: sort people by name, then stably by age, and people of the same age stay in alphabetical order.

In place — the algorithm needs only O(1) (or O(log n)) extra memory besides the array.

Which one to use ​

SituationChoose
General purpose, fastest on averageQuick sort
Must be stable, or guaranteed n log nMerge sort
Guaranteed n log n with O(1) memoryHeap sort
Small or almost sorted arraysInsertion sort
Integers in a small rangeCounting sort
Many integers with few digitsRadix sort
Learning the basicsBubble, Selection

In production JavaScript Array.prototype.sort (TimSort, a hybrid of merge and insertion sort) is usually the right choice. This library shows how the classic algorithms work and gives you reliable implementations for courses, experiments and special cases.

Literature ​

  • CLRS — T. Cormen, C. Leiserson, R. Rivest, C. Stein. Introduction to Algorithms, 4th ed., MIT Press, 2022. Chapters 2, 6, 7, 8.
  • Sedgewick — R. Sedgewick, K. Wayne. Algorithms, 4th ed., Addison-Wesley, 2011. Chapter 2.
  • Knuth — D. Knuth. The Art of Computer Programming, Vol. 3: Sorting and Searching, 2nd ed., 1998.

MIT License