Complete the code to initialize the gap for Shell Sort.
let n = arr.length;
let gap = [1];The gap starts as half the array length, which helps compare distant elements first.
Complete the code to reduce the gap after each pass in Shell Sort.
while (gap > 0) { // sorting logic here gap = [1]; }
The gap is reduced by half each time to gradually compare closer elements.
Fix the error in the inner loop condition to correctly perform insertion sort with the gap.
for (let i = gap; i < n; i++) { let temp = arr[i]; let j = i; while (j >= gap && arr[j - [1]] > temp) { arr[j] = arr[j - gap]; j -= gap; } arr[j] = temp; }
The inner loop compares elements that are 'gap' positions apart, so subtract gap.
Fill both blanks to complete the gap-based insertion sort step inside the while loop.
while (j >= [1] && arr[j - [2]] > temp) { arr[j] = arr[j - gap]; j -= gap; }
Both blanks use 'gap' because the sorting compares and shifts elements gap positions apart.
Fill all three blanks to complete the Shell Sort function.
function shellSort(arr) {
let n = arr.length;
let gap = [1];
while (gap > 0) {
for (let i = gap; i < n; i++) {
let temp = arr[i];
let j = i;
while (j >= [2] && arr[j - [3]] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
gap = Math.floor(gap / 2);
}
return arr;
}The gap starts at half the array length, and the inner loop uses gap to compare and shift elements.