Complete the code to start the outer loop from the second element in the array.
for i := [1]; i < len(arr); i++ { key := arr[i] j := i - 1 for j >= 0 && arr[j] > key { arr[j+1] = arr[j] j-- } arr[j+1] = key }
The insertion sort starts from the second element (index 1) because the first element is considered sorted.
Complete the code to move elements greater than the key one position ahead.
for j := i - 1; j >= 0 && arr[j] [1] key; j-- { arr[j+1] = arr[j] }
We move elements that are greater than the key to the right to make space for the key.
Fix the error in placing the key after shifting elements.
arr[[1]] = keyAfter the inner loop, j is decremented one extra time, so the correct position is j+1.
Fill both blanks to complete the inner loop condition and decrement.
for j := i - 1; j [1] 0 && arr[j] > key; j[2] { arr[j+1] = arr[j] }
The loop continues while j is greater or equal to 0 and arr[j] is greater than key. We decrement j each time.
Fill all three blanks to create a function that sorts an array using insertion sort.
func [1](arr []int) { for i := [2]; i < len(arr); i++ { key := arr[i] j := i - 1 for j >= 0 && arr[j] > key; j[3] { arr[j+1] = arr[j] } arr[j+1] = key } }
The function is named InsertionSort. The outer loop starts at 1. The inner loop decrements j with '--'.