The World of Sorting: Why Do We Need Sorting?

Sorting is everywhere in daily life: supermarkets arrange products by price, mobile phone photo albums sort photos by date, and grade sheets order scores from highest to lowest… In essence, sorting rearranges a set of “disordered” data according to specific rules (such as size or alphabetical order), making the data more organized and easier to find and use.

For programming beginners, the “Three Sorting Brothers”—Bubble, Selection, and Insertion Sort—are the first they encounter. Though simple, they are foundational to understanding more complex algorithms like Quick Sort or Merge Sort, just as addition, subtraction, multiplication, and division are essential in mathematics. Today, we explore these “entry-level contenders” to see which makes the best first “sorting champion.”

I. Bubble Sort: “Bubbling” Up the Largest Number

Core Idea: Like bubbles rising in water, each “bubble” pushes the largest unsorted element to the end, repeating until the entire array is sorted.

Example:
Sort the array [5, 3, 8, 4, 2] in ascending order:
- First Bubble Pass: Compare adjacent elements, swapping larger ones backward:
- 5 and 3 → swap → [3, 5, 8, 4, 2]
- 5 and 8 → no swap
- 8 and 4 → swap → [3, 5, 4, 8, 2]
- 8 and 2 → swap → [3, 5, 4, 2, 8]
Now the largest number 8 is “bubbled” to the end, leaving [3, 5, 4, 2] unsorted.

  • Second Bubble Pass: Repeat for [3, 5, 4, 2] to move the next largest number 5 to the second last position:
  • 3 and 5 → no swap
  • 5 and 4 → swap → [3, 4, 5, 2]
  • 5 and 2 → swap → [3, 4, 2, 5]
    Now the sorted region is [5, 8].

  • Continue until all elements are sorted.

Characteristics:
- Pros: Simple logic, easy to understand (like “line-up bubbling”).
- Cons: Frequent comparisons and swaps; time complexity is O(n²) (e.g., for n=1000, ~1 million operations).
- Optimization: If no swaps occur in a pass, the array is already sorted—terminate early (not included in the basic version).

II. Selection Sort: “Selecting” the Smallest Number

Core Idea: In each iteration, select the smallest unsorted element and place it at the end of the sorted region, gradually building a sorted array.

Example:
Sort [5, 3, 8, 4, 2]:
- First Selection: Find the smallest element 2 in the entire array, swap with the first element → [2, 3, 8, 4, 5]. Sorted region: [2].

  • Second Selection: Find the smallest element 3 in [3, 8, 4, 5] (already in place). Sorted region: [2, 3].

  • Third Selection: Find the smallest element 4 in [8, 4, 5], swap with the third element → [2, 3, 4, 8, 5]. Sorted region: [2, 3, 4].

  • Fourth Selection: Find the smallest element 5 in [8, 5], swap with the fourth element → [2, 3, 4, 5, 8].

Characteristics:
- Pros: Few swaps (at most n-1); efficient for small datasets.
- Cons: Must scan the remaining elements to find the minimum; time complexity is O(n²).
- Detail: Selection Sort is “unstable” (e.g., [2, 2, 1] may become [1, 2, 2] with swapped positions of the two 2s).

III. Insertion Sort: “Inserting” into the Correct Position

Core Idea: Like sorting playing cards by size, insert each new element into its correct position in the already sorted portion, gradually building the sorted array.

Example:
Sort [5, 3, 8, 4, 2]:
- Initial Sorted Portion: [5] (first element).

  • Insert Second Element: Take 3, compare with 5 (last in sorted portion). Since 3 < 5, insert before 5 → [3, 5].

  • Insert Third Element: Take 8, compare with 5 (last in sorted portion). Since 8 > 5, insert at the end → [3, 5, 8].

  • Insert Fourth Element: Take 4, compare backward from the end:

  • 4 < 8 → shift 8; 4 < 5 → shift 5; 4 > 3 → insert after 3 → [3, 4, 5, 8].

  • Insert Fifth Element: Take 2, compare backward:

  • 2 < 8 → shift 8; 2 < 5 → shift 5; 2 < 4 → shift 4; 2 < 3 → shift 3; insert at the start → [2, 3, 4, 5, 8].

Characteristics:
- Pros: Ideal for small datasets or “nearly sorted” data (e.g., mostly ordered student scores); time complexity approaches O(n) for such cases.
- Cons: If data is completely unsorted, swaps are similar to Bubble Sort; time complexity remains O(n²).
- Practical Use: Many programming language standard libraries (e.g., Java’s Arrays.sort) use Insertion Sort for small arrays (length < 47) to optimize performance.

IV. Who Is the “Entry-Level Sorting Champion”?

Compare the three:
- Bubble Sort: Intuitive (like “milking the largest to the end”) but swaps are excessive.
- Selection Sort: Few swaps, suitable for “result-only” scenarios but unstable.
- Insertion Sort: Natural “hand-card organization” logic; efficient for nearly sorted data and foundational to complex sorts (e.g., Shell Sort).

No Absolute Champion: They are “stepping stones” to understanding sorting. Bubble Sort’s “bubbling” inspired Heap Sort, Selection Sort’s “find-min” inspired Heap Sort, and Insertion Sort’s “gradual sorting” inspired Merge Sort and Shell Sort.

For beginners, mastering their core logic (how to make the array “sorted”) is more important than debating “who is faster.” The true “sorting champion” is your understanding of the sorting principles—once you flexibly apply “select-min, insert-right, bubble-max,” any complex algorithm becomes manageable.

Conclusion

Bubble, Selection, and Insertion Sort may be simple, but they are the “foundation bricks” of the programming world. They teach us to build ordered structures through repeated operations—a “gradual optimization” mindset crucial for solving complex problems. Next time you need to sort data, try these three algorithms first, then explore more efficient sorting methods!

Xiaoye