Quick Sort as Divide and Conquer in DSA C - Time & Space Complexity
We want to understand how the time to sort a list using Quick Sort changes as the list gets bigger.
Specifically, how many steps does Quick Sort take when sorting more items?
Analyze the time complexity of the following code snippet.
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
}
int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp;
return i + 1;
}
This code sorts an array by picking a pivot, dividing the array around it, and sorting the parts recursively.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The partition function loops through the array segment once to rearrange elements.
- How many times: The quickSort function calls itself twice on smaller parts, repeating partition on smaller segments until sorted.
When the array size doubles, the number of operations grows more than double but less than square.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 50 to 100 steps |
| 100 | About 700 to 1,000 steps |
| 1000 | About 10,000 to 15,000 steps |
Pattern observation: The steps grow roughly proportional to n times log n, which is faster than simple repeated scanning but slower than quadratic.
Time Complexity: O(n log n)
This means the sorting time grows a bit more than linearly but much less than checking every pair.
[X] Wrong: "Quick Sort always takes the same time regardless of input order."
[OK] Correct: If the pivot choices are poor, Quick Sort can take much longer, even up to n squared steps.
Understanding Quick Sort's time complexity helps you explain why it is fast on average and what can slow it down, showing your grasp of efficient algorithms.
"What if we always picked the middle element as pivot instead of the last? 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 picks a pivot and divides the array into parts smaller and larger than the pivot.Step 2: Recursive sorting of parts
It then sorts these parts recursively, combining results to get a sorted array.Final Answer:
Divide the array into smaller parts using a pivot and sort them recursively -> Option BQuick Check:
Quick Sort = Divide and conquer with pivot [OK]
- Confusing Quick Sort with Merge Sort
- Thinking Quick Sort compares all pairs
- Believing Quick Sort inserts elements one by one
Solution
Step 1: Check parameter types for partition
The partition function takes an array and two indices low and high to work on a subarray.Step 2: Check return type
It returns an integer index where the pivot ends after partitioning.Final Answer:
int partition(int arr[], int low, int high); -> Option AQuick Check:
Partition returns pivot index with array and bounds [OK]
- Using void return type instead of int
- Passing array as int instead of int[]
- Missing one of the index parameters
arr = {3, 6, 8, 10, 1, 2, 1}?Solution
Step 1: Apply Quick Sort to the array
Quick Sort rearranges elements so smaller come before pivot and larger after, recursively sorting subarrays.Step 2: Sort the given array
Sorting {3,6,8,10,1,2,1} results in {1,1,2,3,6,8,10} in ascending order.Final Answer:
[1, 1, 2, 3, 6, 8, 10] -> Option AQuick Check:
Sorted array = ascending order [OK]
- Assuming original array stays same
- Confusing ascending with descending order
- Misplacing duplicate elements
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}Solution
Step 1: Check initialization of i
In partition, i should start at low - 1 to correctly track smaller elements before j.Step 2: Analyze loop and swaps
Starting i at low causes incorrect swaps and wrong pivot placement.Final Answer:
Variable i should start at low - 1 -> Option DQuick Check:
i = low - 1 fixes partition indexing [OK]
- Starting i at low causes off-by-one errors
- Changing pivot position incorrectly
- Loop running beyond high index
Solution
Step 1: Understand problem with duplicates in Quick Sort
Duplicates cause many equal elements to be repeatedly partitioned, slowing sorting.Step 2: Apply three-way partitioning
Three-way partitioning divides array into less than, equal to, and greater than pivot, reducing redundant work.Final Answer:
Use a three-way partitioning to separate less, equal, and greater elements -> Option CQuick Check:
Three-way partitioning optimizes duplicates [OK]
- Picking fixed pivot causes worst-case time
- Skipping duplicates breaks sorting correctness
- Avoiding recursion does not fix duplicate issue
Ready to prove it?
Pass the test and this pattern moves unknown
