0
0
DSA C++programming~10 mins

Insertion Sort Algorithm in DSA C++ - 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 for insertion sort.

DSA C++
for (int i = [1]; i < n; i++) {
    int key = arr[i];
    int j = i - 1;
    while (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'
An
B1
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Starting the loop at 0 causes unnecessary comparisons.
Starting at 2 skips the second element.
2fill in blank
medium

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

DSA C++
while (j >= 0 && arr[j] [1] key) {
    arr[j + 1] = arr[j];
    j--;
}
Drag options to blanks, or click blank then click option'
A<
B==
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes incorrect shifting.
Using '==' does not move elements properly.
3fill in blank
hard

Fix the error in placing the key after shifting elements.

DSA C++
arr[[1]] = key;
Drag options to blanks, or click blank then click option'
Aj + 1
Bi
Cj
Di - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using j places key in wrong position.
Using i or i - 1 does not reflect the shifted position.
4fill in blank
hard

Fill both blanks to complete the insertion sort function header and array size parameter.

DSA C++
void insertionSort(int arr[], int [1]) {
    for (int i = 1; i < [2]; i++) {
        int key = arr[i];
        int j = i - 1;
        while (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'
An
Bsize
Clength
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names causes errors.
Using undefined variables in loop condition.
5fill in blank
hard

Fill all three blanks to print the sorted array after insertion sort.

DSA C++
for (int [1] = 0; [2] < [3]; [1]++) {
    std::cout << arr[[1]] << " ";
}
std::cout << std::endl;
Drag options to blanks, or click blank then click option'
Ai
Bj
Cn
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for loop control.
Using undefined size variable.