CS-251 · Study guide

Sorting algorithms

Compare how sorting algorithms use time and memory. Then follow Quick Sort one step at a time.

Time and space

The table shows how work and extra memory grow when the list gets larger. n is the number of values.

AlgorithmBest caseAverage caseWorst caseExtra space
Selection SortO(n²)O(n²)O(n²)O(1)
Bubble SortO(n)O(n²)O(n²)O(1)
Insertion SortO(n)O(n²)O(n²)O(1)
Merge SortO(n log n)O(n log n)O(n log n)O(n)
Quick SortO(n log n)O(n log n)O(n²)O(log n) typical; O(n) worst

Bubble Sort reaches O(n) in the best case only when it stops early after a pass with no swaps. Quick Sort’s extra space includes memory for its recursive calls.

Quick Sort, step by step

Choose Next to see why each value stays or moves. The last value in each part is the pivot.