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.
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".
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.
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.
Key Takeaways
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.