0
0
DSA C++programming~10 mins

Shell 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 initialize the gap for Shell Sort.

DSA C++
int n = arr.size();
int gap = [1];
Drag options to blanks, or click blank then click option'
An * 2
Bn / 2
C1
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using gap equal to the full array size causes no sorting.
Using gap larger than array size causes errors.
2fill in blank
medium

Complete the code to reduce the gap in each iteration of Shell Sort.

DSA C++
while (gap > 0) {
    // sorting logic here
    gap = [1];
}
Drag options to blanks, or click blank then click option'
Agap - 1
Bgap + 1
Cgap / 2
Dgap * 2
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting 1 reduces gap too slowly.
Multiplying gap increases it, causing infinite loop.
3fill in blank
hard

Fix the error in the inner loop condition to correctly compare elements during insertion sort step.

DSA C++
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;
}
Drag options to blanks, or click blank then click option'
A>
B>=
C<
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes incorrect sorting order.
Using '>=' causes unnecessary shifts.
4fill in blank
hard

Fill both blanks to complete the insertion step inside the gap-based sorting.

DSA C++
int temp = arr[[1]];
int j = [2];
while (j >= gap && arr[j - gap] > temp) {
    arr[j] = arr[j - gap];
    j -= gap;
}
Drag options to blanks, or click blank then click option'
Ai
Bgap
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Using gap as index causes out-of-bound errors.
Using j before initialization causes errors.
5fill in blank
hard

Fill all three blanks to complete the Shell Sort function.

DSA C++
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;
        }
    }
}
Drag options to blanks, or click blank then click option'
An / 2
Bgap / 2
Ci
Dgap
Attempts:
3 left
💡 Hint
Common Mistakes
Using gap as index instead of i.
Increasing gap instead of decreasing causes infinite loop.