QuickSort Partition Method Error
The partition method below is intended for use as part of the QuickSort algorithm. Which statement best describes a logical error in this implementation?
public class QuickSortExample {
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
The index ‘i’ should be initialized to ‘low + 1’ to correctly manage the swap positions.
B
The pivot should be chosen from the beginning of the array (arr[low]) rather than from the end.
C
The for-loop uses ‘j <= high’ instead of ‘j < high’, causing the pivot element (at arr[high]) to be processed within the loop.
D
The final swap is redundant because the pivot is already in the correct location.
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | hiboyshello | 2 | 2 | - | 200 |
| #2 | vld8kitty | 1 | 1 | 0m 35s | 65 |
| #3 | mattaana000 | 1 | 1 | 1m 15s | 25 |
| #4 | lsj08030922 | 0 | 2 | - | -20 |
| #5 | busemagngr | 0 | 1 | 0m 26s | -36 |
Items per page:
10
1 – 5 of 5
