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 algorithm | Average time complexity | Worst time complexity | Stable sort | Characteristics |
|---|---|---|---|---|
| Insertion Sort | O(N²) | O(N²) | O (stable) | Very fast when the data is mostly sorted |
| Bubble Sort | O(N²) | O(N²) | O (stable) | The simplest to implement, but performs poorly |
| Selection Sort | O(N²) | O(N²) | X (unstable) | Few swaps, but always takes the same amount of time |
| Merge Sort | O(N log N) | O(N log N) | O (stable) | Guarantees consistent performance, but needs extra memory |
| Quick Sort | O(N log N) | O(N²) | X (unstable) | The worst case can occur depending on the pivot choice |
| Heap Sort | O(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 algorithm | Time complexity | Stable sort | Characteristics |
|---|---|---|---|
| Radix Sort | O(d × (N + k)) | O (stable) | Repeats distributing values into buckets by each digit (d). Suited to integers and strings of equal length |
| Counting Sort | O(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
Summary
| Concept | Plain-language summary |
|---|---|
| O(N²) sorts | Bubble, selection, insertion. Easy, but slow with lots of data |
| O(N log N) sorts | Merge, quick, heap. The fast sorts used in practice |
| Non-comparison sorts | Radix, counting. The fastest when the range of values is narrow |
| Stable sort | A sort that keeps equal values in their original order |
Source: Codyssey-B1/B5-2 study notes
