Bird
Raised Fist0
DSA Cprogramming~20 mins

Shell Sort Algorithm in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Shell Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Shell Sort on a small array
What is the output of the following Shell Sort implementation on the array [9, 8, 3, 7, 5]?
DSA C
void shellSort(int arr[], int n) {
    for (int gap = n/2; gap > 0; gap /= 2) {
        for (int i = gap; i < n; i++) {
            int temp = arr[i];
            int j;
            for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
                arr[j] = arr[j - gap];
            }
            arr[j] = temp;
        }
    }
}

int main() {
    int arr[] = {9, 8, 3, 7, 5};
    int n = 5;
    shellSort(arr, n);
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
A3 5 7 8 9
B5 3 7 8 9
C9 8 7 5 3
D3 7 5 8 9
Attempts:
2 left
💡 Hint
Shell Sort sorts the array by comparing elements far apart and gradually reducing the gap.
🧠 Conceptual
intermediate
1:30remaining
Understanding the gap sequence in Shell Sort
Which of the following statements about the gap sequence in Shell Sort is true?
AThe gap starts at 1 and doubles each iteration until it reaches the array size.
BThe gap is always fixed at 3 throughout the sorting process.
CThe gap starts at the array size and halves each iteration until it reaches 1.
DThe gap starts at half the array size and halves each iteration until it reaches 1.
Attempts:
2 left
💡 Hint
Think about how the gap reduces to eventually perform a final insertion sort.
🔧 Debug
advanced
2:00remaining
Identify the error in this Shell Sort code snippet
What error will occur when running this Shell Sort code snippet?
DSA C
void shellSort(int arr[], int n) {
    for (int gap = n/2; gap > 0; gap /= 2) {
        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;
        }
    }
}
AThe code will produce a syntax error due to missing semicolons.
BThe code will cause an infinite loop.
CThe array will be sorted in descending order instead of ascending.
DThe code will cause an out-of-bounds array access.
Attempts:
2 left
💡 Hint
Look at the comparison operator in the while loop condition.
📝 Syntax
advanced
1:30remaining
Find the syntax error in this Shell Sort implementation
Which option correctly fixes the syntax error in the following code snippet?
DSA C
void shellSort(int arr[], int n) {
    for (int gap = n/2; gap > 0; gap /= 2) {
        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;
        }
    }
}
AAdd a semicolon after 'int temp = arr[i]'
BChange 'while' to 'for' loop
CReplace 'int j = i;' with 'int j = gap;'
DRemove the inner for loop
Attempts:
2 left
💡 Hint
Check for missing punctuation after variable declarations.
🚀 Application
expert
2:30remaining
Shell Sort gap sequence effect on performance
Which gap sequence is known to provide better average-case performance for Shell Sort compared to the simple n/2 halving sequence?
AUsing gaps of powers of two only (e.g., 8, 4, 2, 1)
BUsing the sequence of gaps based on the formula 3x + 1 (e.g., 1, 4, 13, 40, ...)
CUsing a fixed gap of 5 throughout the sort
DUsing random gap values for each iteration
Attempts:
2 left
💡 Hint
Some gap sequences reduce the number of comparisons and swaps more efficiently.

Practice

(1/5)
1. What is the main idea behind the Shell Sort algorithm?
easy
A. It sorts elements by comparing distant elements and gradually reducing the gap.
B. It uses a divide and conquer approach like merge sort.
C. It builds a heap and extracts the maximum element repeatedly.
D. It repeatedly swaps adjacent elements if they are in wrong order.

Solution

  1. Step 1: Understand Shell Sort concept

    Shell Sort improves insertion sort by comparing elements far apart, not just adjacent ones.
  2. Step 2: Identify the gap reduction

    The algorithm starts with a large gap and reduces it until it becomes 1, sorting elements at each gap.
  3. Final Answer:

    It sorts elements by comparing distant elements and gradually reducing the gap. -> Option A
  4. Quick Check:

    Shell Sort = gap comparison and reduction [OK]
Hint: Remember: Shell Sort uses gaps to compare distant elements [OK]
Common Mistakes:
  • Confusing Shell Sort with merge sort or heap sort
  • Thinking it only swaps adjacent elements like bubble sort
  • Ignoring the gap reduction step
2. Which of the following is the correct syntax to start the gap value in Shell Sort for an array of size n in C?
easy
A. int gap = n * 2;
B. int gap = n - 1;
C. int gap = n / 2;
D. int gap = 1;

Solution

  1. Step 1: Identify typical initial gap

    Shell Sort usually starts with gap as half the array size, so gap = n / 2.
  2. Step 2: Check other options

    Multiplying by 2 or using n-1 is not standard; starting at 1 is too small for initial gap.
  3. Final Answer:

    int gap = n / 2; -> Option C
  4. Quick Check:

    Initial gap = n/2 [OK]
Hint: Start gap at half the array size for Shell Sort [OK]
Common Mistakes:
  • Starting gap at 1 instead of n/2
  • Using n*2 which is too large
  • Using n-1 which is not standard
3. What will be the output array after applying one full pass of Shell Sort with initial gap = 3 on the array [9, 8, 3, 7, 5, 6, 4, 1]?
medium
A. [9, 8, 3, 7, 5, 6, 4, 1]
B. [7, 8, 3, 1, 5, 6, 4, 9]
C. [4, 8, 3, 7, 5, 6, 9, 1]
D. [4, 8, 3, 1, 5, 6, 7, 9]

Solution

  1. Step 1: Identify elements compared with gap 3

    Elements compared are at indices (0,3,6), (1,4,7), and (2,5).
  2. Step 2: Sort elements in these groups

    Group 1: [9,7,4] sorted -> [4, 8, 3, 1, 5, 6, 7, 9]
  3. Step 3: Place sorted groups back

    Index 0->4, 3->7, 6->9
    Index 1->1, 4->5, 7->8
    Index 2->3, 5->6
  4. Step 4: Final array after pass

    [4, 1, 3, 7, 5, 6, 9, 8]
  5. Step 5: Check options

    [4, 8, 3, 1, 5, 6, 7, 9] is [4, 8, 3, 1, 5, 6, 7, 9], which is incorrect.
    [4, 8, 3, 7, 5, 6, 9, 1] is [4, 8, 3, 7, 5, 6, 9, 1], incorrect.
    [7, 8, 3, 1, 5, 6, 4, 9] is [7, 8, 3, 1, 5, 6, 4, 9], incorrect.
    [9, 8, 3, 7, 5, 6, 4, 1] is original array.
    None matches exactly, but the correct output after one pass is [4, 1, 3, 7, 5, 6, 9, 8]. Since not listed, the closest is none, so correct answer must be updated to reflect actual output.
  6. Final Answer:

    [4, 1, 3, 7, 5, 6, 9, 8] -> No option matches exactly, so corrected answer is none of the above.
  7. Quick Check:

    Shell Sort pass with gap 3 rearranges distant elements correctly [OK]
Hint: Group elements by gap and sort each group separately [OK]
Common Mistakes:
  • Sorting entire array instead of gap groups
  • Mixing up indices when placing sorted elements
  • Ignoring elements not in full groups
4. Identify the error in the following Shell Sort code snippet in C:
void shellSort(int arr[], int n) {
  int gap, i, j, temp;
  for (gap = n/2; gap > 0; gap /= 2) {
    for (i = gap; i < n; i++) {
      temp = arr[i];
      for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
        arr[j] = arr[j - gap];
      }
      arr[j] = temp;
    }
  }
}
medium
A. The inner loop condition should be j > gap, not j >= gap.
B. No error; the code correctly implements Shell Sort.
C. The gap update should be gap = gap / 3, not gap /= 2.
D. The variable j is used outside its scope in arr[j] = temp.

Solution

  1. Step 1: Analyze loop conditions and variables

    The inner loop uses j >= gap to ensure correct comparisons and shifting.
  2. Step 2: Check gap update and variable scope

    Using gap /= 2 is standard for Shell Sort; j is declared outside inner loop and accessible.
  3. Final Answer:

    No error; the code correctly implements Shell Sort. -> Option B
  4. Quick Check:

    Code correctness = No error [OK]
Hint: Check loop conditions and variable scopes carefully [OK]
Common Mistakes:
  • Misreading loop boundary conditions
  • Assuming variable scope errors without checking declarations
  • Confusing gap update rules
5. Given an array [23, 12, 1, 8, 34, 54, 2, 3], what will be the final sorted array after applying Shell Sort with gap sequence gap = gap / 2 until gap is 0?
hard
A. [1, 2, 3, 8, 12, 23, 34, 54]
B. [1, 3, 2, 8, 12, 23, 34, 54]
C. [23, 12, 1, 8, 34, 54, 2, 3]
D. [54, 34, 23, 12, 8, 3, 2, 1]

Solution

  1. Step 1: Apply Shell Sort passes with gap sequence

    Start with gap = 8/2 = 4, then 2, then 1.
  2. Step 2: Sort elements at each gap

    Gap 4 pass sorts elements at indices (0,4), (1,5), (2,6), (3,7).
    After gap 4 pass, array partially sorted.
    Gap 2 pass sorts elements at indices (0,2,4,6) and (1,3,5,7).
    Gap 1 pass is normal insertion sort final pass.
  3. Step 3: Final sorted array

    After all passes, array becomes [1, 2, 3, 8, 12, 23, 34, 54].
  4. Final Answer:

    [1, 2, 3, 8, 12, 23, 34, 54] -> Option A
  5. Quick Check:

    Shell Sort final sorted array = ascending order [OK]
Hint: Shell Sort ends with gap 1, which is insertion sort [OK]
Common Mistakes:
  • Stopping before gap reaches 1
  • Not sorting groups correctly at each gap
  • Confusing gap sequence or final order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard