Sorting Algorithms
Comparison Sorting
Master the classic elementary sorting algorithms: Bubble Sort, Selection Sort, and Insertion Sort.
Last Updated: August 2, 2026
•
20 min read
1. Introduction
What are Elementary Comparison Sorts?
Elementary Comparison Sorts refer to Bubble Sort, Selection Sort, and Insertion Sort. These algorithms solve sorting by comparing adjacent or distant elements, performing swaps or shifts until the collection is fully ordered.Why study them?
While modern applications useO(N log N) sorting algorithms, these O(N²) algorithms are critical because:
2. Mental Models
Bubble Sort: Bubbles Rising
Think of elements as gas bubbles in water. The larger, heavier elements "sink" to the bottom, while the smaller, lighter elements "rise" to the top. In each pass through the array, adjacent pairs are compared, and the larger element is swapped forward. By the end of the first pass, the largest element is locked at the end of the array.Selection Sort: Scanning for the Smallest
Imagine sorting a stack of books by size. You scan the entire messy stack to find the absolute smallest book, then swap it with the book at the very first position. Then you scan the remaining books to find the second smallest, swap it to the second position, and so on.Insertion Sort: Organizing a Hand of Cards
Imagine you are being dealt playing cards one by one. You hold a sorted hand. When you receive a new card (e.g., a7), you scan your sorted hand from right to left, shifting larger cards (e.g., 8, 9, 10) to the right to make room, and then insert the 7 into its correct sorted position.
3. Core Concepts & Implementations
Let's dive into the implementations of each comparison sort.
1. Bubble Sort (With Early-Exit Optimization)
Without optimization, Bubble Sort always runs inO(N²) time. By adding a boolean flag swapped, we can terminate the algorithm early if a full pass occurs without any swaps (indicating the array is already sorted).2. Selection Sort
Selection Sort divides the array into sorted and unsorted regions. It repeatedly finds the minimum element from the unsorted region and swaps it to the front. Because it always performsN² comparisons, its time complexity is always O(N²), but it minimizes the number of writes (at most N swaps).3. Insertion Sort
Insertion Sort builds the final sorted array one item at a time. It is highly efficient for small datasets and partially sorted arrays (O(N) best case).4. Visuals & Trace
Let's look at how Insertion Sort processes the input array [5, 2, 9, 1]:
Comparative Analysis Table
| Algorithm | Best Case Time | Avg/Worst Time | Space Complexity | Stable? | In-Place? | Main Characteristic |
|---|---|---|---|---|---|---|
| Bubble Sort | O(N) (adaptive) | O(N²) | O(1) | Yes | Yes | Simple adjacent swaps |
| Selection Sort | O(N²) (non-adaptive) | O(N²) | O(1) | No | Yes | Minimizes swaps (O(N)) |
| Insertion Sort | O(N) (highly adaptive) | O(N²) | O(1) | Yes | Yes | Best for small or pre-sorted lists |
5. Real-World Applications
6. Interview Perspective
How Interviewers Ask This Topic
Interviewers rarely ask you to implement Bubble Sort straight, but they evaluate your understanding of their mechanics:O(N) swaps).K positions away from its target sorted location." -> Use Insertion Sort (O(N × K) complexity, which is O(N) if K is small!).Common Mistakes
Warning: 1. Assuming Selection Sort is Stable: Because Selection Sort swaps elements across long distances, it can easily scramble identical items. For example, sorting
[2a, 2b, 1] swaps 2a with 1, resulting in [1, 2b, 2a], violating stability.> 2. Forgetting to Optimize Bubble Sort: Writing a basic nested Bubble Sort loop that always performs (N²)/2 checks even on fully sorted input shows a lack of complexity optimization.
7. Summary
swapped flag.N.8. Quiz
Question 1: Which of the elementary sorting algorithms is adaptive and runs in O(N) time when the array is already sorted?
Answer: Insertion Sort (and optimized Bubble Sort). Insertion Sort's inner loop checksarr[j] > key. If the array is already sorted, this condition fails immediately on the first check, resulting in a single pass of O(N) operations.
Question 2: What is the primary advantage of Selection Sort over Insertion Sort?
Answer: Selection Sort minimizes the number of swap/write operations. It makes at mostN - 1 swaps, whereas Insertion Sort can make up to O(N²) shifts/writes in the worst case. This is useful for EEPROMs or flash memory where writes are slow or wear down hardware.
Question 3: Why is standard Selection Sort considered unstable? Give a simple counter-example.
Answer: Selection Sort swaps elements over long distances. Example:[5a, 5b, 2]. The minimum is 2. Selection sort swaps 5a with 2, resulting in [2, 5b, 5a]. The relative order of the identical elements 5 is reversed.
Question 4: In a hybrid algorithm like Timsort, why is Insertion Sort used for sorting small subarrays instead of Quick/Merge Sort?
Answer: Because Quick/Merge Sort have recursive call overhead, function frame allocations, and metadata structures. For smallN (e.g., N ≤ 16), the constant factor of Insertion Sort is so small that it runs faster than recursive partitioning/splitting.
Question 5: If you are sorting an array that is almost sorted (only a few elements out of place), which elementary sort is best?
Answer: Insertion Sort. It will run in near-linearO(N) time because the inner shifting loop will exit almost immediately for most elements.