Sorting Algorithms
Sorting algorithms put data into a chosen order, with different methods trading off speed, memory use, and how they handle equal values.
Sorting algorithms arrange items by a key, such as a number, name, or date. Common methods include insertion sort, merge sort, quicksort, and heapsort, each using a different strategy to establish order. For n items, merge sort and heapsort have worst-case running times proportional to n log n, while insertion sort can require time proportional to n². A stable sorting algorithm preserves the original relative order of items with equal keys, which matters when sorting records by multiple fields.
Where this leads
Randomness →
Randomized quicksort chooses pivots randomly, giving an expected running time proportional to n log n for any fixed input order.
Statistics →
Sorting observations makes it straightforward to identify medians, ranks, and other order statistics.
Graph Theory →
Kruskal’s algorithm finds a minimum spanning tree in a weighted undirected graph by processing edges in increasing order of weight.
Data Compression →
The Burrows–Wheeler transform, used in compression, can be constructed by sorting cyclic rotations of the input.