Algorithm|Javascript Algorithm

Sorting Algorithms: More Than Just Ordering Data

3
Sorting Algorithms: More Than Just Ordering Data

Chris is implementing filter features like "Price: Low to High" and "Newest First" for a shopping mall project.

JavaScript provides the invincible .sort() method. Chris writes the code without a second thought.

javascript
const products = [
  { name: "Keyboard", price: 30000 },
  { name: "Mouse", price: 15000 },
  { name: "Monitor", price: 200000 }
];

// Sort by Price Ascending
products.sort((a, b) => a.price - b.price);

The feature works perfectly. But suddenly, he gets curious.

"Why do they ask about Quick Sort or Merge Sort in interviews? In practice, I just use .sort() and I'm done."

"And would this code still be fast if there were 1 million products?"

The reason we need to learn sorting algorithms is not simply to list numbers. It is because sorting is the starting point for all efficient algorithms.

1. Sorting is a Prerequisite for Searching

Imagine we are at a library. If books are scattered on the floor in no particular order, to find a specific book, we have to check them one by one from start to finish (O(N)). If there are 100,000 books, it might take days.

However, what if the books are sorted by 'Genre -> Alphabetical Order'?

We can open a book in the middle, see 'M', and think, "Oh, I need 'A', so it must be way before this," cutting the search range in half. This is Binary Search, and it drastically reduces the time complexity to O(\log N).

In other words, if you invest the cost to sort data once, you save search costs for the rest of the data's life. Sorting is the "Infrastructure Building" phase for handling data efficiently.

2. The Invisible Characteristic: Stability

There is a crucial concept you must consider when choosing a sorting algorithm: Stability (Stable vs. Unstable).

A Stable Sort asks, "Is the relative order of records with equal keys preserved after sorting?"

Scenario: Chris sorts the product list by "Name" first. Then, he sorts it again by "Price".

  • Stable Sort: Products with the same price maintain the "Name" order established in the previous step. (Effect: Sorted by Price, then Name).
  • Unstable Sort: The price is sorted, but the name order gets jumbled up randomly.
  • From a UI perspective, this is vital. When you apply multiple filters in Excel, the order remains consistent thanks to 'Stable Sort'. (Note: JavaScript's .sort() is guaranteed to be stable as of ES2019).

    3. Why Are There So Many Types?

    Bubble, Selection, Insertion, Quick, Merge, Heap... The names vary. Why don't we just use the one "perfect" algorithm?

    It's because "There is no single algorithm perfect for every situation." Each has clear Trade-offs.

  • Bubble, Insertion Sort: Slow (O(N^2)), but easy to implement and extremely fast when data is already nearly sorted.
  • Merge Sort: Consistently fast (O(N log N)), but consumes a lot of memory space (O(N)).
  • Quick Sort: Fastest on average and uses little memory, but can be slow in the worst case (O(N^2)) and is an 'Unstable Sort'.
  • This is why the internal implementation of Array.prototype.sort differs across browsers. (Chrome V8 used to use Quick Sort, but now uses TimSort—a mix of Merge and Insertion Sort—to ensure stability).

    4. Our Learning Journey

    From now on, we will follow the evolution of algorithms.

  • Basics (Bubble, Selection, Insertion): Intuitive but slow. We learn "How to command a computer to sort."
  • Advanced (Merge, Quick, Heap): Introducing the concept of "Divide and Conquer" to drastically boost speed.
  • Special (Radix): Performing magic that sorts numbers without comparing them.
  • Key Takeaways

  • Purpose: Sorting is not just lining things up; it is the prerequisite for Efficient Searching (Binary Search).
  • Stability: Whether the original order of duplicate values is preserved has a significant impact on UX.
  • Diversity: The optimal algorithm changes depending on whether you want to save Memory (Space), increase Speed (Time), or ensure Stability.

  • Now, the first batter is the most famous and easiest one.

    Named because data bubbles up to the top: Bubble Sort. It is the synonym for inefficiency, but there is nothing better for understanding the basic principles of sorting.

    🔗 References

  • Sorting Algorithms Animations
  • MDN - Array.prototype.sort() Stability
  • Comments (0)

    0/1000 characters
    Loading comments...