Complete the code to initialize the gap for Shell Sort.
int n = arr.size();
int gap = [1];The initial gap in Shell Sort is usually set to half the array size to start sorting elements far apart.
Complete the code to reduce the gap in each iteration of Shell Sort.
while (gap > 0) { // sorting logic here gap = [1]; }
Shell Sort reduces the gap by dividing it by 2 each iteration to gradually sort closer elements.
Fix the error in the inner loop condition to correctly compare elements during insertion sort step.
for (int i = gap; i < n; i++) { int temp = arr[i]; int j = i; while (j >= gap && arr[j - gap] [1] temp) { arr[j] = arr[j - gap]; j -= gap; } arr[j] = temp; }
We move elements that are greater than temp to the right to insert temp in the correct position.
Fill both blanks to complete the insertion step inside the gap-based sorting.
int temp = arr[[1]]; int j = [2]; while (j >= gap && arr[j - gap] > temp) { arr[j] = arr[j - gap]; j -= gap; }
We store the current element at index i in temp and start shifting from index j = i.
Fill all three blanks to complete the Shell Sort function.
void shellSort(std::vector<int>& arr) {
int n = arr.size();
for (int gap = [1]; gap > 0; gap = [2]) {
for (int i = gap; i < n; i++) {
int temp = arr[[3]];
int j = i;
while (j >= gap && 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 half each iteration, and uses i as the current index for insertion.