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 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);
}
}
These methods rearrange elements around a pivot to help Quick Sort divide the array.
Both methods use loops to compare and swap elements around the pivot.
- Primary operation: Comparing elements to the pivot and swapping if needed.
- How many times: Each element between low and high is checked once in Lomuto; in Hoare, pointers move inward scanning elements until they cross.
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 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, not constant.
Understanding partition time helps you explain Quick Sort's efficiency and why choosing a good pivot matters.
"What if the pivot is always the smallest element? How would the partition time complexity behave then?"
Practice
Solution
Step 1: Understand Lomuto partition
Lomuto partition uses one 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 around the pivot.Final Answer:
Lomuto uses a single pointer, Hoare uses two pointers moving inward -> Option CQuick Check:
Partition schemes differ by pointer usage = A [OK]
- Confusing pivot selection with partition method
- Thinking both use the same pointer approach
- Assuming Hoare always picks first element as pivot
arr at indices i and j?Solution
Step 1: Understand swapping in C# arrays
Swapping requires a temporary variable to hold one value while assigning the other.Step 2: Check each option
The option "arr[i] = arr[j]; arr[j] = arr[i];" overwrites arr[i] before saving its value. "int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;" correctly uses a temp variable. "swap(arr[i], arr[j]);" and "arr.Swap(i, j);" are invalid syntax in C#.Final Answer:
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; -> Option DQuick Check:
Use temp variable for swap = B [OK]
- Overwriting one element before saving it
- Using non-existent swap functions
- Confusing syntax with other languages
arr = [8, 3, 7, 6, 2] with pivot as last element?Solution
Step 1: Identify pivot and initialize pointers
Pivot is 2 (last element). 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. Finally swap pivot with arr[i+1] = arr[0].Step 3: Final array after swap
Swap arr[0] and arr[4]: [2, 3, 7, 6, 8]Final Answer:
[2, 3, 7, 6, 8] -> Option AQuick Check:
Lomuto partitions around pivot = D [OK]
- Swapping incorrectly during iteration
- Confusing pivot position after partition
- Mixing Hoare and Lomuto steps
int HoarePartition(int[] arr, int low, int high) {
int pivot = arr[low];
int i = low + 1;
int j = high;
while (true) {
while (arr[i] < pivot) i++;
while (arr[j] > pivot) j--;
if (i >= j) return j;
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
}Solution
Step 1: Analyze pointer increments
i increments while arr[i] < pivot without boundary check; similarly j decrements without boundary check.Step 2: Identify risk of out-of-bounds
If all elements are less or greater than pivot, i or j can move beyond array limits causing runtime error.Final Answer:
Pointers i and j can go out of array bounds causing runtime error -> Option BQuick Check:
Missing boundary checks cause errors = A [OK]
- Ignoring boundary checks in loops
- Assuming pivot must be last element
- Misplacing swap and return statements
arr = [10, 80, 30, 90, 40, 50, 70], what is the array state after applying Hoare partition with pivot as arr[low] (first element)?Solution
Step 1: Initialize pointers and pivot
Pivot = 10 (arr[0]), i = low + 1 = 1, j = high = 6 (following the Hoare code style).Step 2: Move pointers inward and swap
i moves forward while arr[i] < pivot: arr[1]=80 not <10, i stays 1. j moves backward while arr[j] > pivot: arr[6]=70 >10, j=5; arr[5]=50 >10, j=4; arr[4]=40 >10, j=3; arr[3]=90 >10, j=2; arr[2]=30 >10, j=1; arr[1]=80 >10, j=0. Now i=1, j=0, i >= j, so partition ends. But before that, swaps happen when i < j. The first swap occurs between arr[i]=80 and arr[j]=70 (positions 1 and 6), then pointers move inward. After completing swaps, the array becomes [70, 30, 40, 50, 10, 80, 90].Step 3: Resulting array after partition
The array after Hoare partition is [70, 30, 40, 50, 10, 80, 90].Final Answer:
[70, 30, 40, 50, 10, 80, 90] -> Option AQuick Check:
Hoare partitions around first element pivot = B [OK]
- Assuming pivot moves to final position immediately
- Confusing Lomuto and Hoare partition results
- Not tracing pointer movements carefully
Ready to prove it?
Pass the test and this pattern moves unknown
