In short
You'll rarely write quicksort at work, but interviewers still ask how sorting works, what it costs, and when sorting first makes the rest of a problem easy.
Algorithms for ordering data (bubble, merge, quick, etc.) — foundational CS knowledge, and the basis for many other algorithms' efficiency assumptions.

Think of it like this
Arranging a bookshelf
Sorting a messy shelf by height, you might split it into two halves, arrange each half, then merge them by always picking the shorter front book. That's merge sort, and it's how you'd actually do it without losing your mind.
Watch it happen
Merge sort on [8, 3, 7, 1]
1/4Split the array into halves: [8, 3] and [7, 1].
The big ideas
What you need to know
O(n log n) is the target
Merge sort, heap sort and quicksort on average all take O(n log n). Bubble and insertion sort are O(n²).
Merge sort is stable
Equal items keep their original order. Useful when you sort by one field after another.
Quicksort picks a pivot
Smaller items go left, bigger go right, then recurse. Fast in practice, but bad pivots make it O(n²).
JS sort compares text
Without a comparator, [10, 9].sort() treats numbers as strings. Always pass (a, b) => a - b.
See it in code
Walk through the code
1const marks = [100, 25, 9, 40];2marks.sort(); // [100, 25, 40, 9] (compared as text!)3marks.sort((a, b) => a - b); // [9, 25, 40, 100]4 5const students = [6 { name: "Riya", cgpa: 8.9 },7 { name: "Aman", cgpa: 9.4 },8 { name: "Zoya", cgpa: 8.9 },9];10// Highest CGPA first; ties keep their original order (sort is stable)11students.sort((a, b) => b.cgpa - a.cgpa);12// Aman, Riya, Zoya13 14// A sorted copy that leaves the original alone (ES2023)15const topFirst = marks.toSorted((a, b) => b - a);1/3The default sort turns numbers into strings. A comparator fixes it.
Array.prototype.sort is guaranteed stable since ES2019, so Riya stays ahead of Zoya when their CGPAs tie.
Save yourself the pain
Common mistakes
Oops
Calling .sort() on numbers with no comparator.
Do this instead
Always write .sort((a, b) => a - b). The default compares strings, so 100 comes before 25.
Oops
Forgetting that sort() changes the original array.
Do this instead
Copy first with [...arr] or use toSorted() if other code still needs the original order.
Your mission
Sort smart
Quick check
What does [10, 9, 100].sort() return in JavaScript?
Go deeper
- 1Implement merge sort and quicksort from scratch, not just call a built-in.
- 2Learn the time/space complexity trade-offs between the common sorting algorithms.
- 3Understand why most languages' built-in sort is a hybrid algorithm, not a single 'pure' one.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.