Challenge - 5 Problems
Shell Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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]));Attempts:
2 left
💡 Hint
Shell Sort sorts elements by comparing elements at a certain gap and reducing the gap gradually.
✗ Incorrect
Shell Sort starts with a large gap and sorts elements that are far apart. Then it reduces the gap and sorts again until the gap is 1, which is a normal insertion sort. This process results in a sorted array.
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about how moving elements far apart early can reduce the total number of moves later.
✗ Incorrect
Starting with a larger gap allows the algorithm to move elements that are far apart closer to their correct positions early, which reduces the total number of comparisons and shifts needed in later passes.
🔧 Debug
advanced2: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]));Attempts:
2 left
💡 Hint
Check the comparison operator in the inner while loop.
✗ Incorrect
The comparison operator in the inner while loop is '<' which causes the array to be sorted in descending order incorrectly. Shell Sort should use '>' to sort in ascending order.
📝 Syntax
advanced1: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 } }
Attempts:
2 left
💡 Hint
JavaScript requires semicolons to separate statements, especially inside loops.
✗ Incorrect
The code is missing semicolons at the end of some statements, which can cause syntax errors or unexpected behavior. Adding semicolons fixes the syntax.
🚀 Application
expert2: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?
Attempts:
2 left
💡 Hint
Nearly sorted arrays benefit from small gaps to quickly fix minor disorder.
✗ Incorrect
For nearly sorted arrays, small gap sequences like (5, 3, 1) help quickly fix the few misplaced elements with minimal overhead, making the sort efficient.