Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to select the pivot element in Lomuto partition.
DSA C++
int lomutoPartition(int arr[], int low, int high) {
int pivot = arr[[1]];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
std::swap(arr[i], arr[j]);
}
}
std::swap(arr[i + 1], arr[high]);
return i + 1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing the pivot as the first element instead of the last.
Using an index outside the array bounds.
✗ Incorrect
In Lomuto partition, the pivot is chosen as the last element, which is at index 'high'.
2fill in blank
mediumComplete the code to initialize the pointer for Hoare partition.
DSA C++
int hoarePartition(int arr[], int low, int high) {
int pivot = arr[low];
int i = [1];
int j = high + 1;
while (true) {
do {
i++;
} while (arr[i] < pivot);
do {
j--;
} while (arr[j] > pivot);
if (i >= j) return j;
std::swap(arr[i], arr[j]);
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing 'i' at low instead of low - 1.
Confusing the start positions of 'i' and 'j'.
✗ Incorrect
In Hoare partition, 'i' starts just before the low index, so it is initialized to low - 1.
3fill in blank
hardFix the error in the Lomuto partition swap condition.
DSA C++
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] [1] pivot) {
i++;
std::swap(arr[i], arr[j]);
}
}
std::swap(arr[i + 1], arr[high]);
return i + 1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<=' causing wrong partitioning.
Using '==' which only moves elements equal to pivot.
✗ Incorrect
The condition arr[j] <= pivot ensures elements less than or equal to pivot are moved left.
4fill in blank
hardFill both blanks to complete the Hoare partition loop conditions.
DSA C++
int hoarePartition(int arr[], int low, int high) {
int pivot = arr[low];
int i = low - 1;
int j = high + 1;
while (true) {
do {
i++;
} while (arr[i] [1] pivot);
do {
j--;
} while (arr[j] [2] pivot);
if (i >= j) return j;
std::swap(arr[i], arr[j]);
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators causing infinite loops.
Mixing up the directions of 'i' and 'j' pointers.
✗ Incorrect
In Hoare partition, 'i' moves forward while arr[i] < pivot, and 'j' moves backward while arr[j] > pivot.
5fill in blank
hardFill all three blanks to complete the Lomuto partition swap and return.
DSA C++
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[1];
std::swap(arr[i], arr[j]);
}
}
std::swap(arr[i [2] 1], arr[[3]]);
return i + 1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i+' instead of 'i++' causing syntax errors.
Swapping with wrong indices causing incorrect partition.
✗ Incorrect
We increment 'i' with '++', swap arr[i + 1] with arr[high], and return i + 1 as the pivot index.