Quick Sort Partition Lomuto and Hoare in DSA C - Time & Space Complexity
We want to understand how the time taken by Quick Sort's partition steps grows as the input size increases.
Specifically, we look at Lomuto and Hoare partition methods to see how their operations scale.
Analyze the time complexity of these two partition functions used in Quick Sort.
// Lomuto Partition
int lomutoPartition(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++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
// Hoare Partition
int hoarePartition(int arr[], int low, int high) {
int pivot = arr[low];
int i = low - 1, j = high + 1;
while (1) {
do { i++; } while (arr[i] < pivot);
do { j--; } while (arr[j] > pivot);
if (i >= j) return j;
swap(&arr[i], &arr[j]);
}
}
These functions rearrange elements around a pivot to help Quick Sort divide the array.
Both partitions use loops to compare and swap elements.
- Primary operation: Comparing elements to the pivot and swapping if needed.
- How many times: Each element between low and high is checked once or twice in the loops.
As the number of elements (n) increases, the partition loops run roughly n times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 comparisons and swaps |
| 100 | About 100 comparisons and swaps |
| 1000 | About 1000 comparisons and swaps |
Pattern observation: The operations grow roughly in direct proportion to the input size.
Time Complexity: O(n)
This means the partition step takes time proportional to the number of elements it processes.
[X] Wrong: "Partitioning takes constant time because it just picks a pivot."
[OK] Correct: Partitioning must check and possibly swap many elements, so its time grows with input size.
Knowing how partitioning scales helps you explain Quick Sort's efficiency clearly and confidently in interviews.
What if the pivot was always chosen as the middle element? How would that affect the partition's time complexity?
Practice
Solution
Step 1: Understand Lomuto partition
Lomuto partition uses a single pointer to track the boundary of smaller elements and swaps elements accordingly.Step 2: Understand Hoare partition
Hoare partition uses two pointers starting from both ends moving inward to swap elements that are on the wrong side.Final Answer:
Lomuto uses one pointer, Hoare uses two pointers moving inward -> Option CQuick Check:
Pointer count difference = A [OK]
- Confusing sorting order difference
- Thinking both use same pointer method
- Assuming Hoare uses recursion only
Solution
Step 1: Check parameter types
Array must be passed as int arr[], and low and high as int indices.Step 2: Check return type
Lomuto partition returns an int index, so return type must be int.Final Answer:
int lomutoPartition(int arr[], int low, int high); -> Option AQuick Check:
Correct signature = A [OK]
- Using void return type instead of int
- Passing array as int arr instead of int arr[]
- Swapping low and high parameters
arr = [8, 3, 7, 6, 2] with low=0 and high=4?Solution
Step 1: Choose pivot and initialize pointers
Pivot is arr[high] = 2. Initialize i = low - 1 = -1.Step 2: Iterate and swap elements smaller than pivot
Traverse j from 0 to 3:
- j=0: arr[0]=8 > 2, no swap
- j=1: arr[1]=3 > 2, no swap
- j=2: arr[2]=7 > 2, no swap
- j=3: arr[3]=6 > 2, no swap
After loop, swap arr[i+1]=arr[0] and pivot arr[4]=2.Final Answer:
[2, 3, 6, 7, 8] -> Option DQuick Check:
Pivot 2 placed at start = A [OK]
- Swapping elements incorrectly with pivot
- Using wrong pivot index
- Confusing i and j pointers
int hoarePartition(int arr[], int low, int high) {
int pivot = arr[low];
int i = low - 1, j = high + 1;
while (1) {
do { i++; } while (arr[i] < pivot);
do { j--; } while (arr[j] > pivot);
if (i >= j) return j;
swap(&arr[i], &arr[j]);
}
}Solution
Step 1: Examine the swap call
The code callsswap(&arr[i], &arr[j]);but theswapfunction is not defined in the snippet, causing a compilation error.Step 2: Verify other components
Pointer initializationi=low-1, j=high+1is correct for do-while structure; pivot at low ok; returnjstandard.Final Answer:
Swap function is missing -> Option BQuick Check:
Undefined swap function = C [OK]
- Missing swap function definition
- Confusing Hoare pointer initialization
- Returning wrong index after partition
[10, 80, 30, 90, 40, 50, 70], what is the final array after applying Hoare partition with low=0 and high=6?Solution
Step 1: Initialize pointers and pivot
Pivot = arr[low] = 10, i = low - 1 = -1, j = high + 1 = 7.Step 2: Move pointers
do { i++; } while (arr[i] < 10); i++ to 0, arr[0]=10 not < 10, i=0.
do { j--; } while (arr[j] > 10); j-- from 7: 6(70>10),5(50>10),4(40>10),3(90>10),2(30>10),1(80>10),0(10 not >10), j=0.
i=0 >= j=0, return 0. No swap.Final Answer:
[10, 80, 30, 90, 40, 50, 70] -> Option AQuick Check:
Array unchanged (all ≥ pivot) = D [OK]
- Assuming full sort after one partition
- Confusing pivot position after partition
- Mis-tracing pointer movements
Ready to prove it?
Pass the test and this pattern moves unknown
