Quick Sort Partition Lomuto and Hoare in DSA Java - 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 compare the Lomuto and Hoare partition methods to see how their operations scale.
Analyze the time complexity of these two partition methods 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, j);
}
}
swap(arr, i + 1, 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 (true) {
do { i++; } while (arr[i] < pivot);
do { j--; } while (arr[j] > pivot);
if (i >= j) return j;
swap(arr, i, j);
}
}
Both methods rearrange elements around a pivot but use different approaches.
Both methods repeatedly compare and swap elements to partition the array.
- Primary operation: Comparing elements to the pivot and swapping if needed.
- How many times: Each element between low and high is checked at least once.
As the number of elements (n) increases, the number of comparisons and swaps grows roughly in proportion to n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 comparisons and some swaps |
| 100 | About 100 comparisons and swaps |
| 1000 | About 1000 comparisons and swaps |
Pattern observation: The operations grow linearly with 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.
Understanding partition time helps you explain Quick Sort's efficiency and shows you can analyze key algorithm steps clearly.
"What if the pivot is always the smallest 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 out of place.Final Answer:
Lomuto uses one pointer, Hoare uses two pointers moving inward -> Option AQuick Check:
Pointer count difference [OK]
- Confusing sorting order difference
- Thinking both use same pointer strategy
- Assuming Hoare uses recursion only
arr?Solution
Step 1: Identify Java swap syntax
Java does not support tuple unpacking or built-in swap method for arrays, so manual swap using a temporary variable is needed.Step 2: Check each option
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; uses a temporary variable to swap correctly; others are invalid Java syntax.Final Answer:
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; -> Option AQuick Check:
Manual swap with temp variable [OK]
- Using Python-style tuple swap
- Calling non-existent swap method
- Trying to swap without temp variable
arr = [8, 3, 7, 6, 2] with pivot as last element?Solution
Step 1: Identify pivot and initialize pointers
Pivot is last element 2. Initialize i = -1.Step 2: Iterate and swap elements smaller than pivot
Check elements: 8 > 2 no swap; 3 > 2 no swap; 7 > 2 no swap; 6 > 2 no swap. Then swap pivot with element at i+1 (index 0): swap arr[0] and arr[4].Final Answer:
[2, 3, 7, 6, 8] -> Option CQuick Check:
Lomuto partition output [OK]
- Not swapping pivot at end
- Confusing pivot position
- Swapping elements incorrectly
int hoarePartition(int[] arr, int low, int high) {
int pivot = arr[low];
int i = low + 1, j = high;
while (true) {
while (i <= high && arr[i] < pivot) i++;
while (j >= low && arr[j] > pivot) j--;
if (i >= j) break;
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
int temp = arr[low]; arr[low] = arr[j]; arr[j] = temp;
return j;
}Solution
Step 1: Check pointer initialization
In Hoare partition, i starts at low + 1 and j at high, which is a valid approach.Step 2: Analyze pointer movement and swapping
The code correctly moves pointers inward and swaps elements until i >= j, then swaps pivot with arr[j].Final Answer:
No error, code is correct -> Option DQuick Check:
Pointer start positions and loop logic [OK]
- Starting pointers outside array incorrectly
- Using wrong pivot position
- Misplacing swap after loop
arr = [5, 3, 8, 4, 2, 7, 1, 10], after applying Hoare partition with pivot as first element, what is the correct partitioned array?Solution
Step 1: Initialize pointers and pivot
Pivot = 5 (first element), i = low - 1 = -1, j = high + 1 = 8.Step 2: Move pointers and swap
i moves right skipping elements < 5: i=0(5), stop; j moves left skipping elements > 5: j=7(10),6(1 stop at 1). Swap arr[i]=5 and arr[j]=1. Array becomes [1,3,8,4,2,7,5,10]. Continue until i >= j.Step 3: Final partitioned array
After all swaps, array is [2, 3, 1, 4, 5, 7, 8, 10] with pivot index at 4.Final Answer:
[2, 3, 1, 4, 5, 7, 8, 10] -> Option BQuick Check:
Hoare partition swaps around pivot [OK]
- Confusing pivot position after partition
- Not swapping correctly when pointers cross
- Assuming sorted order after one partition
Ready to prove it?
Pass the test and this pattern moves unknown
