Bird
Raised Fist0
DSA Pythonprogramming~10 mins

Shell Sort Algorithm in DSA Python - Interactive Practice

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

Complete the code to initialize the gap for Shell Sort.

DSA Python
def shell_sort(arr):
    n = len(arr)
    gap = [1]
    while gap > 0:
        # sorting logic here
        gap //= 2
Drag options to blanks, or click blank then click option'
An // 2
Bn * 2
C1
Dn + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using gap as the full length of the array
Starting gap at 1
2fill in blank
medium

Complete the code to perform the inner loop for comparing elements in Shell Sort.

DSA Python
def shell_sort(arr):
    n = len(arr)
    gap = n // 2
    while gap > 0:
        for i in range(gap, n):
            temp = arr[i]
            j = i
            while j >= [1] and arr[j - gap] > temp:
                arr[j] = arr[j - gap]
                j -= gap
            arr[j] = temp
        gap //= 2
Drag options to blanks, or click blank then click option'
Ai
B0
Cgap
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of gap, causing index errors
Using i which changes each iteration
3fill in blank
hard

Fix the error in the gap update step of Shell Sort.

DSA Python
def shell_sort(arr):
    n = len(arr)
    gap = n // 2
    while gap > 0:
        # sorting logic here
        gap = [1]
Drag options to blanks, or click blank then click option'
Agap // 2
Bgap + 1
Cgap - 1
Dgap * 2
Attempts:
3 left
💡 Hint
Common Mistakes
Increasing gap instead of decreasing
Subtracting 1 which may cause infinite loop
4fill in blank
hard

Fill both blanks to complete the insertion step inside the inner loop of Shell Sort.

DSA Python
def shell_sort(arr):
    n = len(arr)
    gap = n // 2
    while gap > 0:
        for i in range(gap, n):
            temp = arr[i]
            j = i
            while j >= [1] and arr[j - gap] > temp:
                arr[j] = arr[j - gap]
                j -= [2]
            arr[j] = temp
        gap //= 2
Drag options to blanks, or click blank then click option'
A0
B1
Cgap
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of gap for boundary
Decrementing j by 1 instead of gap
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each element to its gap-sorted position if greater than zero.

DSA Python
def shell_sort(arr):
    n = len(arr)
    gap = n // 2
    while gap > 0:
        for i in range(gap, n):
            temp = arr[i]
            j = i
            while j >= gap and arr[j - gap] > temp:
                arr[j] = arr[j - gap]
                j -= gap
            arr[j] = temp
        gap //= 2
    result = {{ [1]: arr[[2]] for [3] in range(n) if [1] > 0 }}
    return result
Drag options to blanks, or click blank then click option'
Ai
Bj
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using j which is not defined here
Using wrong variable names

Practice

(1/5)
1. What is the main idea behind the Shell Sort algorithm?
easy
A. It uses a pivot element to partition the list into smaller parts.
B. It sorts elements by repeatedly swapping adjacent elements until sorted.
C. It divides the list into two halves and sorts each half separately.
D. It sorts elements by comparing and swapping distant elements first, then reducing the gap.

Solution

  1. Step 1: Understand Shell Sort's approach

    Shell Sort starts by sorting pairs of elements far apart from each other, then progressively reduces the gap between elements to be compared.
  2. Step 2: Compare with other sorting methods

    Unlike bubble sort (adjacent swaps) or quicksort (pivot partition), Shell Sort uses a gap sequence to improve efficiency.
  3. Final Answer:

    It sorts elements by comparing and swapping distant elements first, then reducing the gap. -> Option D
  4. Quick Check:

    Shell Sort = Distant element comparisons first [OK]
Hint: Remember: Shell Sort reduces gap size step-by-step [OK]
Common Mistakes:
  • Confusing Shell Sort with bubble sort
  • Thinking it always compares adjacent elements first
  • Mixing it up with quicksort's pivot method
2. Which of the following is the correct way to start the gap sequence in Shell Sort for a list of length n?
easy
A. gap = n
B. gap = 1
C. gap = n // 2
D. gap = n * 2

Solution

  1. Step 1: Identify the initial gap value

    Shell Sort typically starts with a gap of half the list length, i.e., gap = n // 2.
  2. Step 2: Understand gap reduction

    The gap is then reduced by half each iteration until it reaches 0.
  3. Final Answer:

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

    Initial gap = n // 2 [OK]
Hint: Start gap at half the list length [OK]
Common Mistakes:
  • Starting gap at 1 (too small)
  • Starting gap equal to n (too large)
  • Multiplying gap by 2 instead of dividing
3. What will be the output of the following Python code using Shell Sort?
def shell_sort(arr):
    n = len(arr)
    gap = n // 2
    while gap > 0:
        for i in range(gap, n):
            temp = arr[i]
            j = i
            while j >= gap and arr[j - gap] > temp:
                arr[j] = arr[j - gap]
                j -= gap
            arr[j] = temp
        gap //= 2

arr = [9, 8, 3, 7, 5]
shell_sort(arr)
print(arr)
medium
A. [3, 7, 5, 8, 9]
B. [3, 5, 7, 8, 9]
C. [9, 8, 7, 5, 3]
D. [5, 3, 7, 8, 9]

Solution

  1. Step 1: Trace the sorting process

    The code implements Shell Sort correctly. It sorts the list [9, 8, 3, 7, 5] into ascending order.
  2. Step 2: Verify final sorted list

    After sorting, the list becomes [3, 5, 7, 8, 9].
  3. Final Answer:

    [3, 5, 7, 8, 9] -> Option B
  4. Quick Check:

    Sorted list = [3, 5, 7, 8, 9] [OK]
Hint: Shell Sort outputs sorted list in ascending order [OK]
Common Mistakes:
  • Assuming original list remains unchanged
  • Confusing descending with ascending order
  • Misreading the gap reduction loop
4. Identify the error in this Shell Sort implementation:
def shell_sort(arr):
    n = len(arr)
    gap = n // 2
    while gap > 0:
        for i in range(gap, n):
            temp = arr[i]
            j = i
            while j >= gap and arr[j + gap] > temp:
                arr[j] = arr[j - gap]
                j -= gap
            arr[j] = temp
        gap //= 2
medium
A. The inner while condition uses arr[j + gap] instead of arr[j - gap].
B. The gap is not reduced correctly in the loop.
C. The for loop range is incorrect; it should start from 0.
D. The temporary variable 'temp' is not assigned properly.

Solution

  1. Step 1: Check the inner while loop condition

    The condition uses arr[j + gap] which is incorrect; it should compare arr[j - gap] to temp.
  2. Step 2: Confirm correct element shifting

    Using arr[j + gap] can cause index errors or wrong comparisons; arr[j - gap] ensures proper backward comparison.
  3. Final Answer:

    The inner while condition uses arr[j + gap] instead of arr[j - gap]. -> Option A
  4. Quick Check:

    Compare with arr[j - gap], not arr[j + gap] [OK]
Hint: Check index directions carefully in inner loops [OK]
Common Mistakes:
  • Using wrong index direction in comparisons
  • Not reducing gap properly
  • Incorrect loop start index
5. Given the list [23, 12, 1, 8, 34, 54, 2, 3], what will be the state of the list after the first gap iteration (gap = 4) in Shell Sort?
hard
A. [23, 12, 1, 3, 34, 54, 2, 8]
B. [3, 12, 1, 8, 2, 54, 23, 34]
C. [2, 12, 1, 8, 23, 54, 3, 34]
D. [23, 12, 1, 8, 34, 54, 2, 3]

Solution

  1. Step 1: Identify elements compared with gap 4

    Elements compared are pairs 0 & 4, 1 & 5, 2 & 6, 3 & 7.
  2. Step 2: Perform swaps if needed

    Compare and swap if left element > right element:
    Index 0 & 4: 23 > 34? No swap.
    Index 1 & 5: 12 > 54? No swap.
    Index 2 & 6: 1 > 2? No swap.
    Index 3 & 7: 8 > 3? Yes, swap 8 and 3.
  3. Step 3: Result after first gap iteration

    The list becomes [23, 12, 1, 3, 34, 54, 2, 8].
  4. Final Answer:

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

    After gap=4: [23, 12, 1, 3, 34, 54, 2, 8] [OK]
Hint: Check pairs separated by gap, swap if left > right [OK]
Common Mistakes:
  • Not swapping elements correctly at gap distance
  • Confusing final list after full sort with partial iteration
  • Ignoring insertion steps within gap
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