0
0
DSA Goprogramming~10 mins

Insertion Sort Algorithm in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start the outer loop from the second element in the array.

DSA Go
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
}
Drag options to blanks, or click blank then click option'
A0
B-1
C1
Dlen(arr)
Attempts:
3 left
💡 Hint
Common Mistakes
Starting the loop at 0 causes unnecessary comparisons.
Starting at len(arr) causes the loop to never run.
2fill in blank
medium

Complete the code to move elements greater than the key one position ahead.

DSA Go
for j := i - 1; j >= 0 && arr[j] [1] key; j-- {
    arr[j+1] = arr[j]
}
Drag options to blanks, or click blank then click option'
A<
B<=
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' will move smaller elements incorrectly.
Using '==' will not move any elements.
3fill in blank
hard

Fix the error in placing the key after shifting elements.

DSA Go
arr[[1]] = key
Drag options to blanks, or click blank then click option'
Aj+1
Bi
Cj-1
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Using j places the key in the wrong position.
Using i places the key at the current outer loop index without adjustment.
4fill in blank
hard

Fill both blanks to complete the inner loop condition and decrement.

DSA Go
for j := i - 1; j [1] 0 && arr[j] > key; j[2] {
    arr[j+1] = arr[j]
}
Drag options to blanks, or click blank then click option'
A>=
B<=
C--
D++
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes the loop to run incorrectly.
Using '++' increments j, which is wrong here.
5fill in blank
hard

Fill all three blanks to create a function that sorts an array using insertion sort.

DSA Go
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
    }
}
Drag options to blanks, or click blank then click option'
AInsertionSort
B1
C--
DSortArray
Attempts:
3 left
💡 Hint
Common Mistakes
Starting outer loop at 0.
Using '++' instead of '--' for j.
Using unclear function names.