Quick Sort Partition Lomuto and Hoare in DSA Javascript - Time & Space Complexity
We want to understand how the time taken by Quick Sort's partition steps changes as the input size grows.
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
function lomutoPartition(arr, low, high) {
let pivot = arr[high];
let i = low - 1;
for (let j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
[arr[i + 1], arr[high]] = [arr[high], arr[i + 1]];
return i + 1;
}
// Hoare Partition
function hoarePartition(arr, low, high) {
let pivot = arr[low];
let i = low - 1;
let j = high + 1;
while (true) {
do { i++; } while (arr[i] < pivot);
do { j--; } while (arr[j] > pivot);
if (i >= j) return j;
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
These functions rearrange elements around a pivot to help Quick Sort work efficiently.
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, each element visited at most 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 increase linearly as input size grows.
Time Complexity: O(n)
This means the partition step takes time proportional to the number of elements it processes.
[X] Wrong: "Partitioning is a constant time operation because it just picks a pivot."
[OK] Correct: Partitioning compares and swaps many elements, so its time grows with input size, not fixed.
Understanding partition time helps you explain Quick Sort's efficiency clearly, a key skill in interviews.
"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 with the pivot at the end.Step 2: Understand Hoare partition
Hoare partition uses two pointers starting from both ends moving inward to swap elements out of place.Final Answer:
Lomuto uses one pointer, Hoare uses two pointers moving inward -> Option AQuick Check:
Pointer count difference = Lomuto(1), Hoare(2) [OK]
- Confusing the number of pointers used
- Thinking Lomuto sorts differently by order
- Assuming partition depends on array length
arr[i] and arr[j] in JavaScript during partition?Solution
Step 1: Identify correct swap syntax in JavaScript
JavaScript does not support tuple unpacking; swapping requires a temporary variable.Step 2: Check each option
temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; uses a temp variable correctly; B is Python syntax; C is undefined function; D overwrites values incorrectly.Final Answer:
temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; -> Option AQuick Check:
Use temp variable for swap in JS [OK]
- Using Python-style tuple swap in JavaScript
- Calling undefined swap function
- Overwriting values without temp variable
arr = [8, 3, 7, 6, 2] with pivot as last element?Solution
Step 1: Identify pivot and initialize pointers
Pivot = 2 (last element). i starts at -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 CQuick Check:
Lomuto partition places pivot correctly [OK]
- Not swapping pivot at the end
- Swapping elements incorrectly
- Confusing pivot position
function hoarePartition(arr, low, high) {
let pivot = arr[low];
let i = low - 1;
let j = high + 1;
while (true) {
do { i++; } while (arr[i] < pivot);
do { j--; } while (arr[j] > pivot);
if (i >= j) return j;
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}Solution
Step 1: Check initialization of pointers
Hoare partition requires i = low and j = high to start inside the array.Step 2: Analyze effect of incorrect initialization
Standard Hoare partition initializes i = low, j = high, starting the pointers at the array bounds and moving inward.Final Answer:
Initialization of i and j is incorrect; should be i=low, j=high -> Option DQuick Check:
Correct pointer start = i=low, j=high [OK]
- Setting i and j outside array bounds
- Confusing pivot position
- Misusing swap syntax
p?Solution
Step 1: Understand Hoare partition return value
Hoare partition returns an index p such that elements arr[low..p] ≤ pivot and arr[p+1..high] ≥ pivot. The pivot is not guaranteed to be at position p.Step 2: Determine correct recursive calls
Recurse on the left subarray from low to p (inclusive) and right subarray from p+1 to high: quickSort(arr, low, p); quickSort(arr, p + 1, high).Final Answer:
quickSort(arr, low, p); quickSort(arr, p + 1, high); -> Option BQuick Check:
Hoare recursion: low to p, p+1 to high [OK]
- Using p+1 in left recursive call
- Ignoring infinite recursion risk
- Confusing Lomuto and Hoare recursion
Ready to prove it?
Pass the test and this pattern moves unknown
