Quick Sort Algorithm in DSA C - Time & Space Complexity
We want to understand how the time taken by Quick Sort changes as the list size grows.
How does the number of steps grow when sorting bigger lists?
Analyze the time complexity of the following Quick Sort 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; 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.
Look at the parts that repeat as the input grows.
- Primary operation: The for-loop inside
partitionthat compares and swaps elements. - How many times: This loop runs for each element in the current sub-array during each recursive call.
- Recursion: The function calls itself twice on smaller parts after partitioning.
- Dominant operation: The repeated partitioning and recursive calls dominate the time.
Intuitively, Quick Sort splits the list and sorts parts recursively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 to 50 comparisons and swaps |
| 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, roughly like n times log n.
Time Complexity: O(n log n)
This means the time grows a little more than linearly as the list gets bigger, making Quick Sort efficient for large lists.
[X] Wrong: "Quick Sort always runs in linear time because it divides the list quickly."
[OK] Correct: Sometimes the pivot divides the list unevenly, causing more work and making the time closer to quadratic.
Understanding Quick Sort's time complexity helps you explain why it is fast and when it might slow down, a key skill in coding interviews.
"What if we always picked the middle element as pivot instead of the last? How would the time complexity change?"
Practice
Quick Sort algorithm?Solution
Step 1: Understand Quick Sort's approach
Quick Sort picks a pivot element 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 them to get the sorted array.Final Answer:
Divide the array into smaller parts using a pivot and sort them recursively -> Option AQuick Check:
Quick Sort = Divide and conquer [OK]
- Confusing Quick Sort with Bubble Sort
- Thinking Quick Sort merges arrays like Merge Sort
- 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: Confirm return type
It returns an integer index of the pivot after partitioning.Final Answer:
int partition(int arr[], int low, int high); -> Option BQuick Check:
Partition returns pivot index [OK]
- Using void return type for partition
- Passing array as int instead of int[]
- Adding extra parameters like pivot
arr = {3, 6, 8, 10, 1, 2, 1}?Solution
Step 1: Apply Quick Sort logic on the array
Quick Sort rearranges elements around pivots recursively, sorting the array in ascending order.Step 2: Sort the given array
Sorting {3,6,8,10,1,2,1} results in {1,1,2,3,6,8,10}.Final Answer:
[1, 1, 2, 3, 6, 8, 10] -> Option CQuick Check:
Sorted array ascending = [1,1,2,3,6,8,10] [OK]
- Assuming Quick Sort keeps original order
- Confusing ascending with descending order
- Not sorting duplicates correctly
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.Step 2: Analyze loop and swaps
Starting i at low causes skipping the first element comparison and wrong swaps.Final Answer:
Variable i should start at low - 1, not low -> Option AQuick Check:
i = low - 1 fixes partition indexing [OK]
- Starting i at low causes incorrect partitioning
- Changing pivot position unnecessarily
- Extending loop beyond high index
Solution
Step 1: Understand problem with duplicates in Quick Sort
Standard Quick Sort can degrade with many duplicates due to repeated swaps and partitions.Step 2: Apply 3-way partitioning
3-way partitioning divides array into three parts: less than, equal to, and greater than pivot, reducing unnecessary work.Final Answer:
Use 3-way partitioning to separate less than, equal to, and greater than pivot -> Option DQuick Check:
3-way partitioning optimizes duplicates [OK]
- Choosing fixed pivot without randomization
- Skipping duplicates without partitioning
- Switching to slower sorts unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
