Complete the code to initialize the gap for Shell Sort.
int n = arr.length;
int gap = [1];The Shell Sort starts with a gap of half the array length to compare elements far apart.
Complete the code to reduce the gap in each iteration of Shell Sort.
while (gap > 0) { // sorting logic gap = gap [1] 2; }
The gap is reduced by dividing it by 2 in each iteration to gradually sort the array.
Fix the error in the inner loop condition to correctly compare elements in Shell Sort.
for (int i = gap; i < n; i++) { int temp = arr[i]; int j = i; while (j >= [1] && arr[j - gap] > temp) { arr[j] = arr[j - gap]; j -= gap; } arr[j] = temp; }
The inner loop must continue while j is greater than or equal to 0 to avoid index errors.
Fill both blanks to complete the insertion step inside the Shell Sort inner loop.
while (j >= [1] && arr[j - gap] > temp) { arr[j] = arr[[2]]; j -= gap; }
The loop continues while j is at least 0, and elements are shifted from index j-gap to j.
Fill all three blanks to complete the Shell Sort method.
public void shellSort(int[] arr) {
int n = arr.length;
for (int gap = [1]; gap > 0; gap = gap [2] 2) {
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j = i;
while (j >= [3] && arr[j - gap] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
}
}The Shell Sort starts with gap = n/2, reduces gap by dividing by 2, and the inner loop boundary is zero.
