📖 Scenario: You are helping a friend understand how the Quick Sort algorithm divides an array into parts using two popular methods: Lomuto and Hoare partition schemes. This is like organizing a deck of cards by picking a card (pivot) and arranging others around it.
🎯 Goal: Build two functions to partition a list of numbers using Lomuto and Hoare methods, then see how the list looks after each partition.
📋 What You'll Learn
Create a list called numbers with exact values [8, 3, 7, 6, 2, 5, 4, 1]
Create a variable called pivot_index set to the last index of numbers
Write a function lomuto_partition(numbers, low, high) implementing Lomuto partition scheme
Write a function hoare_partition(numbers, low, high) implementing Hoare partition scheme
Print the list after applying Lomuto partition
Print the list after applying Hoare partition
💡 Why This Matters
🌍 Real World
Quick Sort is used in many software systems to sort data efficiently, such as organizing files, searching databases, and preparing data for analysis.
💼 Career
Understanding partition schemes helps in optimizing sorting algorithms, which is important for software engineers working on performance-critical applications.
Progress0 / 4 steps
1
Create the list of numbers
Create a list called numbers with these exact values: [8, 3, 7, 6, 2, 5, 4, 1]
DSA Python
Hint
Use square brackets to create a list and separate numbers with commas.
2
Set the pivot index
Create a variable called pivot_index and set it to the last index of the numbers list using len(numbers) - 1
DSA Python
Hint
Use len(numbers) - 1 to get the last index.
3
Write Lomuto and Hoare partition functions
Write two functions: lomuto_partition(numbers, low, high) and hoare_partition(numbers, low, high) implementing Lomuto and Hoare partition schemes respectively. Use numbers list and low, high indices as parameters. Follow these rules:
Lomuto: - Choose pivot as numbers[high] - Initialize i = low - 1 - Loop j from low to high - 1 - If numbers[j] <= pivot, increment i and swap numbers[i] and numbers[j] - Swap numbers[i + 1] and numbers[high] - Return i + 1
Hoare: - Choose pivot as numbers[low] - Initialize i = low - 1 and j = high + 1 - Loop forever: - Increment i until numbers[i] >= pivot - Decrement j until numbers[j] <= pivot - If i >= j, return j - Swap numbers[i] and numbers[j]
DSA Python
Hint
Follow the step-by-step rules for each partition method carefully. Use swaps to rearrange elements.
4
Print the list after Lomuto and Hoare partitions
Call lomuto_partition(numbers, 0, pivot_index) and print the numbers list. Then reset numbers to [8, 3, 7, 6, 2, 5, 4, 1], call hoare_partition(numbers, 0, pivot_index) and print the numbers list again.
DSA Python
Hint
Remember to reset the numbers list before calling the second partition function. Use print(numbers) to show the list after each partition.
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.