What is the main idea behind the Lomuto partition scheme in Quick Sort?
Lomuto partition picks the last element as pivot and rearranges the array so that elements smaller than pivot come before it, and larger come after. It returns the pivot's final position.
Click to reveal answer
intermediate
How does the Hoare partition scheme differ from Lomuto's in Quick Sort?
Hoare partition uses two pointers starting from both ends moving towards each other, swapping elements to ensure left side is smaller and right side is larger than pivot. It returns an index near the pivot but not necessarily the pivot's final position.
Click to reveal answer
beginner
In Lomuto partition, what happens when an element is smaller than the pivot during iteration?
When an element is smaller than the pivot, it is swapped with the element at the 'store index', and the store index is incremented. This keeps smaller elements to the left.
Click to reveal answer
intermediate
Why can Hoare partition be more efficient than Lomuto partition?
Hoare partition generally does fewer swaps and comparisons because it moves pointers inward and swaps only when necessary, making it faster especially on large arrays.
Click to reveal answer
intermediate
What is the return value of Hoare partition and how is it used in Quick Sort?
Hoare partition returns an index where the left side elements are less than or equal to pivot and right side elements are greater than or equal. Quick Sort then recursively sorts the two subarrays divided by this index.
Click to reveal answer
Which element does Lomuto partition scheme choose as pivot?
AFirst element
BLast element
CMiddle element
DRandom element
✗ Incorrect
Lomuto partition always picks the last element as the pivot.
In Hoare partition, pointers start from:
ABoth ends moving inward
BOnly from the left
COnly from the right
DMiddle moving outward
✗ Incorrect
Hoare partition uses two pointers starting from both ends and moving towards each other.
What does Lomuto partition return after partitioning?
ANumber of swaps
BLeft pointer index
CRight pointer index
DFinal pivot index
✗ Incorrect
Lomuto partition returns the final position of the pivot element.
Which partition scheme generally performs fewer swaps?
ALomuto
BBoth equal
CHoare
DDepends on pivot
✗ Incorrect
Hoare partition usually performs fewer swaps than Lomuto.
After Hoare partition, Quick Sort recursively sorts:
ATwo subarrays divided by returned index
BOnly left subarray
COnly right subarray
DEntire array again
✗ Incorrect
Quick Sort recursively sorts the two parts divided by the index returned by Hoare partition.
Explain step-by-step how Lomuto partition rearranges an array around the pivot.
Think about how smaller elements are moved before the pivot.
You got /6 concepts.
Describe how Hoare partition uses two pointers to partition the array and what it returns.
Focus on pointer movements and swapping.
You got /6 concepts.
Practice
(1/5)
1. What is the main difference between Lomuto and Hoare partition schemes in Quick Sort?
easy
A. Lomuto sorts the array, Hoare only partitions without sorting
B. Lomuto uses a single pointer to partition, Hoare uses two pointers moving inward
C. Hoare uses recursion, Lomuto does not
D. Lomuto always picks the last element as pivot, Hoare picks the first element
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 to partition, Hoare uses two pointers moving inward -> Option B
Quick Check:
Partition schemes differ by pointer usage = A [OK]
Hint: Remember: Lomuto one pointer, Hoare two pointers [OK]
Common Mistakes:
Confusing pivot selection with partition method
Thinking Hoare does not partition
Assuming Lomuto uses two pointers
2. Which of the following is the correct syntax for Lomuto partition function header in Python?
easy
A. def lomuto_partition(arr):
B. def lomuto_partition(arr, start, end):
C. def lomuto_partition(arr, low, high):
D. def lomuto_partition(low, high, arr):
Solution
Step 1: Check parameter order and names
Lomuto partition typically takes array, low index, and high index as parameters in that order.
Step 2: Validate function header syntax
def lomuto_partition(arr, low, high): matches the common and correct syntax: def lomuto_partition(arr, low, high):
Final Answer:
def lomuto_partition(arr, low, high): -> Option C
Quick Check:
Correct function header = C [OK]
Hint: Remember parameter order: array, low, high [OK]
Common Mistakes:
Swapping parameter order
Missing parameters
Using incorrect parameter names
3. What is the output of the following code using Lomuto partition?
def lomuto_partition(arr, low, high):
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i+1], arr[high] = arr[high], arr[i+1]
return i+1
arr = [8, 3, 7, 6, 2]
index = lomuto_partition(arr, 0, 4)
print(arr)
medium
A. [2, 3, 6, 7, 8]
B. [3, 2, 7, 6, 8]
C. [8, 3, 7, 6, 2]
D. [2, 3, 7, 6, 8]
Solution
Step 1: Identify pivot and initial pointers
Pivot is arr[4] = 2. i starts at -1.
Step 2: Iterate and swap elements <= pivot
Check elements from index 0 to 3:
- arr[0]=8 > 2 no swap
- arr[1]=3 > 2 no swap
- arr[2]=7 > 2 no swap
- arr[3]=6 > 2 no swap
No i increment, i stays -1.