0
0
DSA Javascriptprogramming~10 mins

Shell Sort Algorithm in DSA Javascript - 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 Javascript
let n = arr.length;
let gap = [1];
Drag options to blanks, or click blank then click option'
A1
Bn
CMath.floor(n / 2)
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Setting gap to 0 causes no sorting steps.
Setting gap to n means no initial gap reduction.
2fill in blank
medium

Complete the code to reduce the gap after each pass in Shell Sort.

DSA Javascript
while (gap > 0) {
  // sorting logic here
  gap = [1];
}
Drag options to blanks, or click blank then click option'
AMath.floor(gap / 2)
Bgap - 1
Cgap * 2
Dgap + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Increasing the gap causes infinite loops.
Subtracting 1 may not reduce gap properly for large arrays.
3fill in blank
hard

Fix the error in the inner loop condition to correctly perform insertion sort with the gap.

DSA Javascript
for (let i = gap; i < n; i++) {
  let temp = arr[i];
  let j = i;
  while (j >= gap && arr[j - [1]] > temp) {
    arr[j] = arr[j - gap];
    j -= gap;
  }
  arr[j] = temp;
}
Drag options to blanks, or click blank then click option'
Agap
B1
Ci
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 causes incorrect comparisons and sorting.
Using i or n causes index errors.
4fill in blank
hard

Fill both blanks to complete the gap-based insertion sort step inside the while loop.

DSA Javascript
while (j >= [1] && arr[j - [2]] > temp) {
  arr[j] = arr[j - gap];
  j -= gap;
}
Drag options to blanks, or click blank then click option'
Agap
B1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of gap causes wrong element comparisons.
Using 0 causes index errors.
5fill in blank
hard

Fill all three blanks to complete the Shell Sort function.

DSA Javascript
function shellSort(arr) {
  let n = arr.length;
  let gap = [1];
  while (gap > 0) {
    for (let i = gap; i < n; i++) {
      let temp = arr[i];
      let j = i;
      while (j >= [2] && arr[j - [3]] > temp) {
        arr[j] = arr[j - gap];
        j -= gap;
      }
      arr[j] = temp;
    }
    gap = Math.floor(gap / 2);
  }
  return arr;
}
Drag options to blanks, or click blank then click option'
AMath.floor(n / 2)
Bgap
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of gap in the while loop causes incorrect sorting.
Not initializing gap properly causes no sorting.