Merge sort just splits the array in the middle. Its hard work comes at the end, when it merges. Quick sort is the opposite: its hard work comes at the start, when it splits. So when the parts are joined again, they only need to be put side by side.
Quick sort picks one number as the pivot (a reference number). Then the other numbers are separated: numbers smaller than the pivot go left, bigger ones go right. Both sides are then sorted the same way. It's like lining people up by height. Pick one person. Shorter people stand on their left, taller people on their right, then each group is arranged again the same way. The person you picked is the pivot, and a person's height is a number's value.
Pick a pivot, split, repeat
Here's what happens to [10, 7, 3, 9, 1, 5, 8], with the last number of each part as the pivot:
| Part | Pivot | Left (smaller) | Right (bigger) |
|---|---|---|---|
[10, 7, 3, 9, 1, 5, 8] |
8 | [7, 3, 1, 5] |
[10, 9] |
[7, 3, 1, 5] |
5 | [3, 1] |
[7] |
[3, 1] |
1 | [] |
[3] |
[10, 9] |
9 | [] |
[10] |
A part with 0 or 1 numbers is always sorted, so it doesn't need to be split again. That's the base case. Then build the result, starting from the shortest parts, in the order left, pivot, right: [1, 3], then [1, 3, 5, 7], then [1, 3, 5, 7, 8, 9, 10]. Look at the first row of the table. After one split, how many numbers are sure to be in their final place? Just one, the pivot 8. Every smaller number is on its left, and every bigger number is on its right.
Here is quick sort in JavaScript, with the last number as the pivot:
function quickSort(arr) {
if (arr.length <= 1) return arr; // base case
const pivot = arr[arr.length - 1]; // pivot: the last number
const rest = arr.slice(0, -1); // every number except the pivot
const left = rest.filter(x => x < pivot); // smaller
const right = rest.filter(x => x >= pivot); // bigger or equal
return [...quickSort(left), pivot, ...quickSort(right)];
}
console.log(quickSort([10, 7, 3, 9, 1, 5, 8]));
// [1, 3, 5, 7, 8, 9, 10]
arr.slice(0, -1)takes every number except the last one, the pivot. The pivot has to be taken out first so it doesn't get split again.filtermakes a new array of the numbers that meet a condition.x => x < pivotis the condition: for each numberx, isxsmaller than the pivot? Soleftgets the numbers< pivot, andrightgets the numbers>= pivot. Numbers equal to the pivot (duplicates) must go to one of the sides. If they don't, they disappear.[...quickSort(left), pivot, ...quickSort(right)]builds the result: the sorted left side, the pivot, then the sorted right side....pours an array's items into a new array.- The pivot can be any number. If you use the first number, the pivot is
arr[0]and the rest isarr.slice(1).
The Lab's version: partitioning in place
The Lab's code doesn't make new arrays. Its partition function separates the numbers inside the array itself. That's why it's called partitioning (separating) in place:
partitionwalks from left to right.- Every number smaller than the pivot is swapped into a row at the front of the array.
- Finally, the pivot is swapped into the spot right after that row.
With pivot 8, [10, 7, 3, 9, 1, 5, 8] becomes [7, 3, 1, 5, 8, 9, 10]. 7, 3, 1, 5 move to the front, and 8 sits at index 4, its final place. This saves memory, because there's no extra array apart from the recursion's own record (the list of function calls that haven't finished).
Fast, but with a worst case
Big-O says how fast the number of steps grows as the data grows.
- O(n log n) on average. The pivot usually splits the data roughly in half. So there are only about log n levels (one level is one round of splitting every part), and each level compares n numbers. 1,000 numbers take about 10,000 steps.
- O(n²) in the worst case. The worst case happens when the pivot is always the smallest or largest number. One example is sorted data with the last number as the pivot. Each split sets aside just one number, the pivot, and all the other numbers go to one side. 1,000 numbers take about 500,000 comparisons.
- The cure: pick a random pivot, or the middle number. On sorted data, a pivot from the middle still splits the data in half. But with many duplicates the cure does not help, because numbers equal to the pivot always go into
right. Split into three:< pivot,=== pivot,> pivot. - The recursion can go n calls deep. On 10,000 sorted or equal numbers, the code above stops with
RangeError: Maximum call stack size exceeded. The built-insorthas neither problem.
Quick sort is not stable: the original order of equal values can change.
⚠️ Lost duplicates, and a pivot that comes back
>on the right side. Ifleftuses< pivotandrightuses> pivot, numbers equal to the pivot go nowhere.[3, 6, 8, 10, 1, 2, 1]loses one of its 1s: the pivot is 1, and the other 1 goes into neitherleftnorright.- Not taking the pivot out. If
rightis taken fromarrinstead ofrest, the pivot lands inrightagain. For data like[2, 2],rightis always the same as the starting array, so the recursion never finishes:Maximum call stack size exceeded. - Forgetting
return. Without thereturnon the last line,quickSort(left)gives backundefined, and...undefinedthrows an error.
TL;DR: Quick sort picks one number as the pivot (a reference number). Numbers smaller than the pivot are moved to the left, and bigger or equal ones to the right. Both sides are sorted with recursion (quickSort calls itself), then the result is built as left, pivot, right: [...quickSort(left), pivot, ...quickSort(right)]. After one split, the pivot is in its final place. On average quick sort is O(n log n). Its worst case is O(n²), when the pivot is always the smallest or largest, like sorted data with the pivot at one end. So use a random pivot or the middle number, and a three-way split (<, ===, >) when there are many duplicates. Quick sort isn't stable: the original order of equal values can change.
⚡ Pick a pivot, split, repeat
Write:
function quickSort(arr) {
if (arr.length <= 1) return arr; // base case
const pivot = arr[arr.length - 1]; // pivot: the last number
const rest = arr.slice(0, -1); // every number except the pivot
const left = rest.filter(x => x < pivot); // smaller
const right = rest.filter(x => x >= pivot); // bigger or equal
return [...quickSort(left), pivot, ...quickSort(right)];
}
Meaning: Quick sort picks a pivot (a reference number), here the last number. The other numbers (rest) that are smaller than the pivot go into left, and bigger or equal ones go into right. Both sides are sorted again by calling quickSort (recursion), then put together: left, pivot, right. After one split, the pivot is in its final place.
⚡ >= on the right, the pivot taken out
Guess first: if right uses x > pivot, what happens to [3, 6, 8, 10, 1, 2, 1]? The pivot is the last number, 1.
One of its 1s goes missing. The other 1 is equal to the pivot, so it goes neither left (< pivot) nor right (> pivot).
Meaning: right is taken from rest (every number except the pivot), not from arr. If the pivot goes into right again, data like [2, 2] makes the recursion never finish.
⚡ Fast on average, but with a worst case
Meaning: On average quick sort is O(n log n), because the pivot usually splits the data roughly in two. But when the pivot is always the smallest or largest, like sorted data with the pivot at one end, quick sort becomes O(n²). That's its worst case. The fix: pick a random pivot or the middle number, and a three-way split (<, ===, >) when there are many duplicates.