Quick Sort as Divide and Conquer in DSA Javascript - Time & Space Complexity
We want to understand how the time taken by Quick Sort changes as the list size grows.
How does dividing and sorting parts affect the total work done?
Analyze the time complexity of the following Quick Sort code.
function quickSort(arr) {
if (arr.length <= 1) return arr;
const pivot = arr[arr.length - 1];
const left = [];
const right = [];
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] < pivot) left.push(arr[i]);
else right.push(arr[i]);
}
return [...quickSort(left), pivot, ...quickSort(right)];
}
This code sorts an array by picking a pivot, splitting the array into smaller parts, and sorting those parts recursively.
Look for loops and recursive calls that repeat work.
- Primary operation: Looping through the array to split it around the pivot.
- How many times: This splitting happens at each recursive call on smaller parts.
Each time, the array is split and sorted recursively, so the work depends on how balanced the splits are.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 to 50 operations |
| 100 | About 700 to 1000 operations |
| 1000 | About 10,000 to 15,000 operations |
Pattern observation: The operations grow a bit faster than linear but much slower than quadratic, depending on how the array splits.
Time Complexity: O(n log n)
This means the time grows a little more than linearly as the list gets bigger, thanks to dividing the problem into smaller parts.
[X] Wrong: "Quick Sort always takes quadratic time because it uses loops inside recursion."
[OK] Correct: The splitting usually divides the array well, so the total work adds up to n log n, not n squared.
Understanding Quick Sort's time complexity shows you can analyze divide and conquer methods, a key skill for many coding challenges.
"What if the pivot always picked the smallest element? How would the time complexity change?"
Practice
Quick Sort algorithm in the divide and conquer approach?Solution
Step 1: Understand divide and conquer in Quick Sort
Quick Sort divides the array into parts based on a pivot, sorts each part recursively.Step 2: Compare with other sorting methods
Unlike bubble or selection sort, Quick Sort uses division and recursion, not just swapping or selecting smallest.Final Answer:
Divide the array into smaller parts, sort them, and combine the results -> Option AQuick Check:
Quick Sort = Divide and conquer [OK]
- Confusing Quick Sort with bubble or selection sort
- Thinking Quick Sort merges arrays like merge sort
- Ignoring the pivot selection step
Solution
Step 1: Identify how to select pivot in Quick Sort
The pivot is usually an element from the array, often the first or last element.Step 2: Check each option's correctness
const pivot = arr[0]; selects the first element as pivot correctly. Others use wrong methods or properties.Final Answer:
const pivot = arr[0]; -> Option DQuick Check:
Pivot selection = arr[0] [OK]
- Using array length as pivot
- Calling array methods instead of accessing element
- Confusing pivot with sorted array
const arr = [4, 2, 7, 1];
const pivot = arr[0]; // 4
let left = [];
let right = [];
for(let i = 1; i < arr.length; i++) {
if(arr[i] <= pivot) left.push(arr[i]);
else right.push(arr[i]);
}
console.log(left);
console.log(right);Solution
Step 1: Identify pivot and partition logic
Pivot is 4. Elements less or equal to 4 go to left, others to right.Step 2: Partition the array
Elements: 2 <= 4 -> left, 7 > 4 -> right, 1 <= 4 -> left. So left = [2,1], right = [7].Final Answer:
[2, 1] and [7] -> Option CQuick Check:
Partition by pivot 4 = [2,1] left, [7] right [OK]
- Including pivot in left or right arrays
- Mixing up less than and greater than conditions
- Forgetting to start loop from index 1
function partition(arr) {
const pivot = arr[0];
let left = [];
let right = [];
for(let i = 0; i < arr.length; i++) {
if(arr[i] < pivot) left.push(arr[i]);
else right.push(arr[i]);
}
return [...left, pivot, ...right];
}Solution
Step 1: Analyze loop range and pivot usage
The loop starts at 0, so pivot element arr[0] is included in left or right arrays again.Step 2: Correct loop start index
Loop should start at 1 to skip pivot element and avoid duplication.Final Answer:
Loop should start from 1 to avoid including pivot twice -> Option BQuick Check:
Loop index = 1 to exclude pivot [OK]
- Starting loop at 0 causing pivot duplication
- Changing pivot position unnecessarily
- Misunderstanding spread operator usage
[3, 6, 8, 10, 1, 2, 1], what is the correct sorted array after applying Quick Sort fully?Solution
Step 1: Understand Quick Sort fully sorts the array
Quick Sort recursively partitions and sorts all parts until fully sorted.Step 2: Sort the array manually
Sorted array is [1, 1, 2, 3, 6, 8, 10], including duplicates in order.Final Answer:
[1, 1, 2, 3, 6, 8, 10] -> Option AQuick Check:
Quick Sort output = sorted array ascending [OK]
- Ignoring duplicate elements
- Confusing sorted with original array
- Thinking Quick Sort reverses array
Ready to prove it?
Pass the test and this pattern moves unknown
