Challenge - 5 Problems
Quick Sort Partition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Lomuto Partition on a sample array
What is the output array after applying Lomuto partition on the array [8, 3, 7, 6, 2, 5, 4] with pivot as the last element?
DSA Python
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 arr arr = [8, 3, 7, 6, 2, 5, 4] lomuto_partition(arr, 0, len(arr)-1) print(arr)
Attempts:
2 left
💡 Hint
Remember Lomuto partition places elements smaller or equal to pivot on left side and swaps pivot to correct position.
✗ Incorrect
Lomuto partition uses the last element as pivot. It moves all elements smaller or equal to pivot to the left, then places pivot after them. The resulting array is [3, 2, 4, 6, 8, 5, 7].
❓ Predict Output
intermediateOutput of Hoare Partition on a sample array
What is the output array after applying Hoare partition on the array [9, 4, 8, 3, 1, 2, 5] with pivot as the first element?
DSA Python
def hoare_partition(arr, low, high): pivot = arr[low] i = low - 1 j = high + 1 while True: i += 1 while arr[i] < pivot: i += 1 j -= 1 while arr[j] > pivot: j -= 1 if i >= j: return j arr[i], arr[j] = arr[j], arr[i] arr = [9, 4, 8, 3, 1, 2, 5] hoare_partition(arr, 0, len(arr)-1) print(arr)
Attempts:
2 left
💡 Hint
Hoare partition uses the first element as pivot and swaps elements to ensure left side smaller and right side larger than pivot.
✗ Incorrect
Hoare partition swaps elements from left and right until pointers cross. The pivot 9 ends up on the right side. The array becomes [5, 4, 8, 3, 1, 2, 9].
🧠 Conceptual
advancedDifference in pivot placement between Lomuto and Hoare partitions
Which statement correctly describes the difference in pivot placement after Lomuto and Hoare partition schemes?
Attempts:
2 left
💡 Hint
Think about where the pivot ends after partitioning in each scheme.
✗ Incorrect
Lomuto partition always places the pivot at its correct sorted position after partitioning. Hoare partition only ensures elements are partitioned but pivot may not be at final position.
🔧 Debug
advancedIdentify the error in Lomuto partition implementation
What error will occur when running this Lomuto partition code on array [4, 5, 3, 7, 2]?
def lomuto_partition(arr, low, high):
pivot = arr[high]
i = low
for j in range(low, high):
if arr[j] < pivot:
arr[i], arr[j] = arr[j], arr[i]
i += 1
arr[i], arr[high] = arr[high], arr[i]
return i
lomuto_partition([4, 5, 3, 7, 2], 0, 4)
Attempts:
2 left
💡 Hint
Check the initialization of i and the comparison operator in the loop.
✗ Incorrect
Starting i at low is valid; using '<' excludes pivot duplicates on left but no error occurs. The code runs correctly and returns pivot index 0.
🚀 Application
expertNumber of swaps in Hoare partition
Given the array [10, 7, 8, 9, 1, 5] and using Hoare partition with pivot as first element, how many swaps occur during partitioning?
DSA Python
def hoare_partition(arr, low, high): pivot = arr[low] i = low - 1 j = high + 1 swaps = 0 while True: i += 1 while arr[i] < pivot: i += 1 j -= 1 while arr[j] > pivot: j -= 1 if i >= j: return swaps arr[i], arr[j] = arr[j], arr[i] swaps += 1 arr = [10, 7, 8, 9, 1, 5] hoare_partition(arr, 0, len(arr)-1)
Attempts:
2 left
💡 Hint
Trace the pointers i and j and count each swap carefully.
✗ Incorrect
Swaps occur when elements on wrong sides are exchanged. For this array, 1 swap happens before pointers cross.
