0
0
DSA Javascriptprogramming~20 mins

Shell Sort Algorithm in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 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 with a specific gap sequence
What is the output array after running the Shell Sort algorithm on the array [23, 12, 1, 8, 34, 54, 2, 3] using the gap sequence [4, 2, 1]?
DSA Javascript
function shellSort(arr) {
  let gaps = [4, 2, 1];
  for (let gap of gaps) {
    for (let i = gap; i < arr.length; i++) {
      let temp = arr[i];
      let j = i;
      while (j >= gap && arr[j - gap] > temp) {
        arr[j] = arr[j - gap];
        j -= gap;
      }
      arr[j] = temp;
    }
  }
  return arr;
}

console.log(shellSort([23, 12, 1, 8, 34, 54, 2, 3]));
A[1, 3, 2, 8, 12, 23, 34, 54]
B[1, 2, 3, 8, 12, 23, 34, 54]
C[12, 1, 2, 3, 8, 23, 34, 54]
D[23, 12, 1, 8, 34, 54, 2, 3]
Attempts:
2 left
💡 Hint
Shell Sort sorts elements by comparing elements at a certain gap and reducing the gap gradually.
🧠 Conceptual
intermediate
1:30remaining
Understanding the gap sequence effect in Shell Sort
Which statement best describes the effect of the gap sequence on the Shell Sort algorithm's performance?
AA larger initial gap helps move elements farther apart closer to their final position early, improving efficiency.
BThe gap sequence does not affect the sorting performance; it only changes the order of comparisons.
CA smaller initial gap leads to faster sorting because fewer elements are compared.
DUsing a gap sequence of only 1 is the most efficient way to perform Shell Sort.
Attempts:
2 left
💡 Hint
Think about how moving elements far apart early can reduce the total number of moves later.
🔧 Debug
advanced
2:00remaining
Identify the error in this Shell Sort implementation
What error does the following Shell Sort code produce when run?
DSA Javascript
function shellSort(arr) {
  let n = arr.length;
  let gap = Math.floor(n / 2);
  while (gap > 0) {
    for (let i = gap; i < n; i++) {
      let temp = arr[i];
      let j = i;
      while (j >= gap && arr[j - gap] < temp) {
        arr[j] = arr[j - gap];
        j -= gap;
      }
      arr[j] = temp;
    }
    gap = Math.floor(gap / 2);
  }
  return arr;
}

console.log(shellSort([5, 3, 8, 4, 2]));
AThe code runs but outputs an incorrectly sorted array [8, 5, 4, 3, 2]
BThe code runs and outputs [5, 3, 8, 4, 2]
CThe code runs and outputs [2, 3, 4, 5, 8]
DThe code runs and outputs [8, 5, 4, 3, 2]
Attempts:
2 left
💡 Hint
Check the comparison operator in the inner while loop.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in this Shell Sort snippet
Which option correctly fixes the syntax error in the following code snippet?
DSA Javascript
for (let gap = Math.floor(arr.length / 2); gap > 0; gap = Math.floor(gap / 2)) {
  for (let i = gap; i < arr.length; i++) {
    let temp = arr[i]
    let j = i
    while (j >= gap && arr[j - gap] > temp) {
      arr[j] = arr[j - gap]
      j -= gap
    }
    arr[j] = temp
  }
}
AReplace 'let' with 'var' for all variable declarations.
BRemove the Math.floor calls from the gap calculation.
CChange 'while' to 'if' in the inner loop condition.
DAdd semicolons at the end of lines missing them inside the loops.
Attempts:
2 left
💡 Hint
JavaScript requires semicolons to separate statements, especially inside loops.
🚀 Application
expert
2:00remaining
Shell Sort on a large nearly sorted array
Given a nearly sorted array of 1000 elements where only 10 elements are out of place, which gap sequence would likely give the best performance for Shell Sort?
AA gap sequence with random gaps unrelated to array size
BA gap sequence of only 1 (normal insertion sort)
CA gap sequence with small gaps only (e.g., 5, 3, 1)
DA gap sequence that starts large and reduces by half each time (e.g., 500, 250, 125, ... 1)
Attempts:
2 left
💡 Hint
Nearly sorted arrays benefit from small gaps to quickly fix minor disorder.