0
0
DSA C++programming~20 mins

Shell Sort Algorithm in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Shell Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Shell Sort with a specific gap sequence
What is the output array after running the Shell Sort algorithm on the input array [23, 12, 1, 8, 34, 54, 2, 3] using the gap sequence [4, 2, 1]?
DSA C++
void shellSort(int arr[], int n) {
    int gaps[] = {4, 2, 1};
    for (int gap : gaps) {
        for (int i = gap; i < n; i++) {
            int temp = arr[i];
            int j = i;
            while (j >= gap && arr[j - gap] > temp) {
                arr[j] = arr[j - gap];
                j -= gap;
            }
            arr[j] = temp;
        }
    }
}

int main() {
    int arr[] = {23, 12, 1, 8, 34, 54, 2, 3};
    int n = sizeof(arr)/sizeof(arr[0]);
    shellSort(arr, n);
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    return 0;
}
A[1, 2, 3, 8, 12, 23, 34, 54]
B[1, 3, 2, 8, 12, 23, 34, 54]
C[2, 3, 1, 8, 12, 23, 34, 54]
D[23, 12, 1, 8, 34, 54, 2, 3]
Attempts:
2 left
💡 Hint
Shell Sort sorts the array by comparing elements at a certain gap and reducing the gap gradually until it becomes 1.
🧠 Conceptual
intermediate
1:30remaining
Understanding the role of gap sequence in Shell Sort
Why does Shell Sort use a gap sequence instead of just sorting adjacent elements like Insertion Sort?
ABecause it avoids sorting the array completely.
BBecause sorting elements far apart first reduces the total number of swaps needed later.
CBecause it only works on arrays with even length.
DBecause it makes the algorithm run slower and more complex.
Attempts:
2 left
💡 Hint
Think about how sorting far apart elements can help bring the array closer to sorted faster.
🔧 Debug
advanced
2:00remaining
Identify the error in this Shell Sort implementation
What error will this Shell Sort code produce when run?
DSA C++
void shellSort(int arr[], int n) {
    int gap = n / 2;
    while (gap > 0) {
        for (int i = gap; i < n; i++) {
            int temp = arr[i];
            int j = i;
            while (j >= gap && arr[j - gap] < temp) {
                arr[j] = arr[j - gap];
                j -= gap;
            }
            arr[j] = temp;
        }
        gap /= 2;
    }
}
AThe code will not compile due to syntax errors.
BThe code will cause a runtime error due to invalid array access.
CThe array will be sorted in descending order, not ascending.
DThe array will remain unsorted.
Attempts:
2 left
💡 Hint
Check the comparison operator inside the inner while loop.
📝 Syntax
advanced
1:30remaining
Find the syntax error in this Shell Sort code snippet
Which option correctly fixes the syntax error in the following code? for (int gap : gaps) { for (int i = gap; i < n; i++) { int temp = arr[i] int j = i; while (j >= gap && arr[j - gap] > temp) { arr[j] = arr[j - gap]; j -= gap; } arr[j] = temp; } }
DSA C++
int gaps[] = {5, 3, 1};
int n = 10;
int arr[10];
ARemove the semicolon after 'arr[j] = temp;'
BChange 'for (int gap : gaps)' to 'for int gap in gaps'
CReplace 'while' with 'if' in the inner loop
DAdd a semicolon after 'int temp = arr[i]'
Attempts:
2 left
💡 Hint
Check for missing punctuation after variable declarations.
🚀 Application
expert
2:30remaining
Choosing the best gap sequence for Shell Sort
Which gap sequence is known to provide better average performance for Shell Sort compared to simple halving (n/2, n/4, ...)?
AUsing the sequence of gaps as the Ciura sequence (e.g., 701, 301, 132, 57, 23, 10, 4, 1)
BUsing the sequence of gaps as powers of two (e.g., 8, 4, 2, 1)
CUsing the sequence of gaps as Fibonacci numbers (e.g., 13, 8, 5, 3, 2, 1)
DUsing the sequence of gaps as prime numbers only (e.g., 17, 13, 7, 3, 1)
Attempts:
2 left
💡 Hint
Some gap sequences are experimentally proven to reduce the number of comparisons and swaps.