In programming, sorting is one of the most fundamental and commonly used operations. Whether processing student grades, product prices, or log data, sorting helps us organize information quickly. Today, we’ll explore two simple sorting algorithms: Insertion Sort and Bubble Sort, examining how they work and their differences.
Insertion Sort: The Art of “Inserting” One by One¶
The core idea of Insertion Sort is intuitive, like organizing a deck of cards—process each card from left to right and insert each new card into its correct position among the already sorted cards in hand. For example, with an unsorted array [5, 3, 8, 4, 2], the steps are as follows:
1. Initial State¶
Assume the first card 5 is already in hand (the “sorted portion”), and the remaining cards 3, 8, 4, 2 need to be inserted.
2. Insert the Second Card 3¶
- Compare
3with the last card in the sorted portion (5):3 < 5, so shift5right by one position to make space for3. - The sorted portion becomes
[3, 5].
3. Insert the Third Card 8¶
- Compare
8with the last card in the sorted portion (5):8 > 5, so place it at the end. - The sorted portion becomes
[3, 5, 8].
4. Insert the Fourth Card 4¶
- Compare
4with elements in the sorted portion from right to left: - Compare with
8:4 < 8→ shift8right; - Compare with
5:4 < 5→ shift5right; - Compare with
3:4 > 3→ found the correct position. Insert4to get[3, 4, 5, 8].
5. Insert the Fifth Card 2¶
- Compare
2with elements in the sorted portion from right to left: - Compare with
8:2 < 8→ shift8right; - Compare with
5:2 < 5→ shift5right; - Compare with
4:2 < 4→ shift4right; - Compare with
3:2 < 3→ shift3right; - Insert at the beginning. The final sorted array is
[2, 3, 4, 5, 8].
Algorithm Summary:
Insertion Sort iterates from the second element onward. For each element key, it compares backward with the sorted portion, shifts elements right, and inserts key at the correct position. The pseudocode is:
for i from 1 to n-1: // Start from the second element
key = arr[i]
j = i - 1 // Index of the last element in the sorted portion
while j >= 0 and arr[j] > key:
arr[j+1] = arr[j] // Shift elements right
j = j - 1
arr[j+1] = key // Insert key into its correct position
Bubble Sort: The “Adventure” of Adjacent Elements¶
Bubble Sort’s core idea is “compare adjacent elements and let the larger ones ‘bubble up’ to the end”. Imagine air bubbles rising in water: the largest bubble first reaches the surface (end of the array), followed by the second largest, until all elements are sorted.
Bubble Sort Example (Using [5, 3, 8, 4, 2]):¶
- First Pass: Compare adjacent elements from the start, bubbling larger elements to the end:
5vs3:5 > 3→ swap →[3, 5, 8, 4, 2];5vs8:5 < 8→ no swap;8vs4:8 > 4→ swap →[3, 5, 4, 8, 2];-
8vs2:8 > 2→ swap →[3, 5, 4, 2, 8].
After this pass, the largest element8is bubbled to the end. -
Second Pass: Process the first 4 elements (excluding the last
8): 3vs5: no swap;5vs4:5 > 4→ swap →[3, 4, 5, 2, 8];-
5vs2: swap →[3, 4, 2, 5, 8].
After this pass, the second largest element5is in place. -
Third Pass: Process the first 3 elements (excluding the last two):
3vs4: no swap;-
4vs2: swap →[3, 2, 4, 5, 8].
After this pass, the third largest element4is in place. -
Fourth Pass: Process the first 2 elements (excluding the last three):
3vs2: swap →[2, 3, 4, 5, 8].
After this pass, the array is fully sorted.
Algorithm Summary:
Bubble Sort requires n-1 passes (where n is the array length). Each pass compares adjacent elements and ensures the largest unplaced element “bubbles” to its final position.
Insertion Sort vs Bubble Sort: Which is Better?¶
| Comparison | Insertion Sort | Bubble Sort |
|---|---|---|
| Core Idea | Build the sorted sequence incrementally (insert unsorted elements) | Gradually “bubble” larger elements to the end |
| Sorting Process | Sorted portion grows from 1 element to the full array | Determine one largest element per pass |
| Time Complexity | Average O(n²), Best O(n) (when array is already sorted) | Average O(n²), Best O(n) (with optimization for sorted arrays) |
| Space Complexity | O(1) (in-place sorting) | O(1) (in-place sorting) |
| Stability | Stable (equal elements retain order) | Stable (equal elements retain order) |
| Use Case | Small datasets or nearly sorted data | Small datasets (rarely used in practice) |
Conclusion¶
Both Insertion Sort and Bubble Sort are simple O(n²) algorithms, but Insertion Sort is more efficient in practice, especially for nearly sorted data. Bubble Sort is intuitive but involves more element swaps (each comparison may swap), while Insertion Sort only shifts elements once per insertion, reducing swap operations.
For beginners, understanding these two sorting methods (insertion vs. bubbling) is foundational for mastering more complex algorithms like Quick Sort or Merge Sort.