Complete the code to initialize the gap for Shell Sort.
def shell_sort(arr): n = len(arr) gap = [1] while gap > 0: # sorting logic here gap //= 2
The gap starts as half the length of the array to begin sorting distant elements first.
Complete the code to perform the inner loop for comparing elements in Shell Sort.
def shell_sort(arr): n = len(arr) gap = n // 2 while gap > 0: for i in range(gap, n): temp = arr[i] j = i while j >= [1] and arr[j - gap] > temp: arr[j] = arr[j - gap] j -= gap arr[j] = temp gap //= 2
The inner while loop continues as long as j is at least gap to avoid index out of bounds when accessing arr[j - gap].
Fix the error in the gap update step of Shell Sort.
def shell_sort(arr): n = len(arr) gap = n // 2 while gap > 0: # sorting logic here gap = [1]
The gap should be reduced by half each time to gradually sort closer elements.
Fill both blanks to complete the insertion step inside the inner loop of Shell Sort.
def shell_sort(arr): n = len(arr) gap = n // 2 while gap > 0: for i in range(gap, n): temp = arr[i] j = i while j >= [1] and arr[j - gap] > temp: arr[j] = arr[j - gap] j -= [2] arr[j] = temp gap //= 2
The loop continues while j is at least gap, and j moves back by the gap size each time.
Fill all three blanks to create a dictionary comprehension that maps each element to its gap-sorted position if greater than zero.
def shell_sort(arr): n = len(arr) gap = n // 2 while gap > 0: for i in range(gap, n): temp = arr[i] j = i while j >= gap and arr[j - gap] > temp: arr[j] = arr[j - gap] j -= gap arr[j] = temp gap //= 2 result = {{ [1]: arr[[2]] for [3] in range(n) if [1] > 0 }} return result
The dictionary comprehension maps index i to the element at arr[i] for all i greater than zero.
