Skip to content

Sorting Algorithms at a Glance ​

This page compares 8 sorting algorithms in a single table. Click a name to see each algorithm's detailed behavior and Python code.

6 Comparison Sorts ​

These sort by comparing values to see "which one is larger."

Sorting algorithmAverage time complexityWorst time complexityStable sortCharacteristics
Insertion SortO(N²)O(N²)O (stable)Very fast when the data is mostly sorted
Bubble SortO(N²)O(N²)O (stable)The simplest to implement, but performs poorly
Selection SortO(N²)O(N²)X (unstable)Few swaps, but always takes the same amount of time
Merge SortO(N log N)O(N log N)O (stable)Guarantees consistent performance, but needs extra memory
Quick SortO(N log N)O(N²)X (unstable)The worst case can occur depending on the pivot choice
Heap SortO(N log N)O(N log N)X (unstable)An efficient sort that needs no extra memory

2 Non-Comparison Sorts ​

These do not compare values with each other; instead, they find each value's place by looking at digits or counting occurrences. When the conditions fit, they are faster than O(N log N).

Sorting algorithmTime complexityStable sortCharacteristics
Radix SortO(d × (N + k))O (stable)Repeats distributing values into buckets by each digit (d). Suited to integers and strings of equal length
Counting SortO(N + k)O (stable)Counts how many times each value appears. Only advantageous when the range of values (k) is small

Terminology ​

  • Average time complexity: the average number of operations, assuming every possible input appears evenly (= typical performance)
  • Worst time complexity: the maximum number of operations when the most unfavorable data pattern comes in (= guaranteed worst-case performance)
  • Stable sort: when several items share the same sort key, their relative order before sorting is preserved after sorting
    • Example) If you first sort products by recommendation and then sort them again by price, products with the same price keep their recommendation order
  • In-place sort: a sort that uses almost no extra memory beyond the input array (bubble, selection, insertion, heap, quick)

How big the difference between O(N²) and O(N log N) is, is explained in the time complexity article.

Handwritten notes comparing sorting algorithms and terminology

Handwritten notes: comparing sorting algorithms and terminology

Summary ​

ConceptPlain-language summary
O(N²) sortsBubble, selection, insertion. Easy, but slow with lots of data
O(N log N) sortsMerge, quick, heap. The fast sorts used in practice
Non-comparison sortsRadix, counting. The fastest when the range of values is narrow
Stable sortA sort that keeps equal values in their original order

Source: Codyssey-B1/B5-2 study notes