How Sorting Algorithms Implement Ascending/Descending Order? A Guide for Data “Obedience”

I. Why Should Data “Obey”? — The Significance of Sorting

Imagine you have a messy deck of cards (e.g., [3, 1, 4, 2]) and want to quickly find the “smallest card” or “largest card”. Flipping through them is too tedious! This is where sorting algorithms come in—they make data “obey” by arranging it in your desired order (ascending or descending).

In simple terms:
- Ascending Order: Arranged from smallest to largest (e.g., [1, 2, 3, 4]).
- Descending Order: Arranged from largest to smallest (e.g., [4, 3, 2, 1]).
Sorting algorithms are the “rules” that make data “obey”, with different algorithms moving data in distinct ways, but all ultimately achieving the desired order.

II. Bubble Sort: Let Data “Float” or “Sink” Like Bubbles

Principle

Bubble Sort is like bubbles in water: larger bubbles “float up” (larger numbers move to the end), and smaller bubbles “sink down” (smaller numbers move to the front). Each round “bubbles up” the largest remaining number to the end until the entire array is sorted.

Ascending Implementation (Smallest to Largest)

Using the array [3, 1, 4, 2] as an example:

  1. First Round: Start from the first element and compare adjacent numbers. Swap if the previous number is larger than the next (to move larger numbers to the end).
    - Compare 3 and 1: 3 > 1 → Swap → [1, 3, 4, 2]
    - Compare 3 and 4: 3 < 4 → No swap
    - Compare 4 and 2: 4 > 2 → Swap → [1, 3, 2, 4]
    - Now the largest number (4) has “bubbled up” to the end.

  2. Second Round: Ignore the already sorted last element (4) and continue comparing the first three elements.
    - Compare 1 and 3: 1 < 3 → No swap
    - Compare 3 and 2: 3 > 2 → Swap → [1, 2, 3, 4]
    - Now the second largest number (3) has “bubbled up” to the second last position. The array is sorted.

Core Rule: For ascending order, swap if the previous number > next number to move larger numbers “up” (to the right).

Descending Implementation (Largest to Smallest)

Reverse the swap condition: Swap if the previous number < next number (to move smaller numbers to the end).

Using [3, 1, 4, 2] again:

  1. First Round: Compare adjacent numbers. Swap if the previous number < next number (to move smaller numbers to the end).
    - Compare 3 and 1: 3 > 1 → No swap
    - Compare 1 and 4: 1 < 4 → Swap → [3, 4, 1, 2]
    - Compare 1 and 2: 1 < 2 → Swap → [3, 4, 2, 1]
    - Now the smallest number (1) has “sunk” to the end.

  2. Second Round: Ignore the sorted last element (1) and compare the first three elements.
    - Compare 3 and 4: 3 < 4 → Swap → [4, 3, 2, 1]
    - Now the second smallest number (2) has “sunk” to the second last position. The array is sorted.

Core Rule: For descending order, swap if the previous number < next number to move smaller numbers “down” (to the right).

III. Selection Sort: Like “Choosing a Monitor” to Fix Positions

Principle

Selection Sort is like selecting the “smallest monitor” first, placing it at the front, then the “second smallest” at the next position, and so on until sorted.

Ascending Implementation (Smallest to Largest)

Using [3, 1, 4, 2]:

  1. Find the smallest number: Traverse the array to find the minimum (1), swap it with the first element → [1, 3, 4, 2].
  2. Find the second smallest number: Traverse from the second element to find the minimum (2), swap it with the second element → [1, 2, 4, 3].
  3. Find the third smallest number: Traverse from the third element to find the minimum (3), swap it with the third element → [1, 2, 3, 4].

Core Logic: In each round, identify the smallest remaining number and place it in its correct position (left to right).

Descending Implementation (Largest to Smallest)

Instead of finding the smallest, find the largest number in each round and place it at the front:

Using [3, 1, 4, 2]:

  1. Find the largest number: Traverse to find the maximum (4), swap with the first element → [4, 1, 3, 2].
  2. Find the second largest number: Traverse from the second element to find the maximum (3), swap with the second element → [4, 3, 1, 2].
  3. Find the third largest number: Traverse from the third element to find the maximum (2), swap with the third element → [4, 3, 2, 1].

Core Rule: For descending order, each round selects the largest remaining number and places it in its correct position (left to right).

IV. Insertion Sort: Like “Organizing Playing Cards” by Inserting One by One

Principle

Insertion Sort is like arranging a hand of cards: each new card is inserted into the correct position among the already sorted cards. For example, if you have [1, 3] and draw a 2, insert it between 1 and 3 to get [1, 2, 3].

Ascending Implementation (Smallest to Largest)

Using [3, 1, 4, 2]:

  1. Initial sorted hand: Only [3].
  2. Insert 1: 1 < 3, so insert before 3 → [1, 3].
  3. Insert 4: 4 > 3, insert at the end → [1, 3, 4].
  4. Insert 2: 2 < 4 → insert before 4; 2 < 3 → insert before 3 → [1, 2, 3, 4].

Core Logic: Split the array into “sorted” and “unsorted” parts. Take elements from the unsorted part and insert them into the correct position in the sorted part.

Descending Implementation (Largest to Smallest)

Insert into the sorted part such that the sorted part remains in descending order:

Using [3, 1, 4, 2]:

  1. Initial sorted hand: [3].
  2. Insert 1: 1 < 3, insert at the end → [3, 1].
  3. Insert 4: 4 > 3, insert at the front → [4, 3, 1].
  4. Insert 2: 2 > 1 → insert before 1; 2 < 3 → insert between 3 and 1 → [4, 3, 2, 1].

Core Rule: For descending order, insert the new element into the sorted part where it fits in the descending sequence.

V. Key to Data “Obedience”: Algorithm “Swap/Insert Rules”

Algorithm Ascending Core Rule Descending Core Rule Metaphor (How Data “Obeys”)
Bubble Sort Larger numbers “bubble up” (swap if prev > next) Smaller numbers “sink down” (swap if prev < next) Bubbles rising (ascending) / sinking (descending)
Selection Sort Select the smallest number each round and place left Select the largest number each round and place left Choose the smallest as “leader”, then the next
Insertion Sort Insert into the correct position in the sorted part Insert into the correct position in the sorted part (descending) Organize cards by inserting new cards in order

VI. Beginner’s Tips

  • Master Basic Algorithms First: Bubble, Selection, and Insertion sorts are foundational. Understanding their “movement logic” makes more complex algorithms (e.g., Quick Sort, Merge Sort) easier to learn.
  • Simulate with Examples: For each algorithm, manually trace steps with a simple array (e.g., [5, 3, 8, 1]) to see how data moves to its correct position.
  • “Obedience” = Clear Algorithm Logic: Whether ascending or descending, the direction of data movement depends on the comparison rule (> or <). Remembering this rule ensures data “obeys”!

Sorting algorithms are a programming “basic skill,” like learning to walk before running. By practicing with examples, data will naturally “obey” your commands!

Xiaoye