QuickSort Partition Condition
In the partition method for QuickSort provided below, what potential issue arises if the condition in the if statement is changed from arr[j] < pivot to arr[j] <= pivot?
public class SortUtil {
public static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = low;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
}
int temp = arr[i];
arr[i] = arr[high];
arr[high] = temp;
return i;
}
}
A
It would optimize the partitioning by reducing the number of swaps.
B
It has no effect since both conditions yield the same result.
C
It could incorrectly reposition duplicate pivot elements, compromising the partitioning process.
D
It would cause a compilation error due to mismatched types.
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE