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 on a small array
What is the output of the following Go code after applying Shell Sort on the array?
DSA Go
package main import "fmt" func shellSort(arr []int) { n := len(arr) for gap := n / 2; gap > 0; gap /= 2 { for i := gap; i < n; i++ { temp := arr[i] j := i for j >= gap && arr[j-gap] > temp { arr[j] = arr[j-gap] j -= gap } arr[j] = temp } } } func main() { arr := []int{23, 12, 1, 8, 34, 54, 2, 3} shellSort(arr) fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Shell Sort sorts by comparing elements far apart and gradually reducing the gap.
✗ Incorrect
Shell Sort starts with a large gap and sorts elements that are far apart. It reduces the gap until it becomes 1, performing a final insertion sort. The final sorted array is in ascending order.
🧠 Conceptual
intermediate1:30remaining
Gap sequence in Shell Sort
Which of the following gap sequences is used in the provided Shell Sort implementation?
Attempts:
2 left
💡 Hint
Look at how the gap variable changes in the code.
✗ Incorrect
The code starts with gap = n/2 and divides gap by 2 each iteration until gap becomes 0.
🔧 Debug
advanced2:00remaining
Identify the error in this Shell Sort variant
What error will this Go code produce when running the Shell Sort variant?
DSA Go
package main import "fmt" func shellSort(arr []int) { n := len(arr) for gap := n / 2; gap > 0; gap /= 2 { for i := gap; i < n; i++ { temp := arr[i] j := i for j >= 0 && arr[j-gap] > temp { arr[j] = arr[j-gap] j -= gap } arr[j] = temp } } } func main() { arr := []int{5, 3, 8, 6, 2} shellSort(arr) fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Check the condition in the inner loop comparing j and gap.
✗ Incorrect
The inner loop condition uses 'j >= 0' instead of 'j >= gap', causing an index out of range panic when j-gap < 0.
❓ Predict Output
advanced2:00remaining
Shell Sort output with negative and positive numbers
What is the output of the following Go code after sorting?
DSA Go
package main import "fmt" func shellSort(arr []int) { n := len(arr) for gap := n / 2; gap > 0; gap /= 2 { for i := gap; i < n; i++ { temp := arr[i] j := i for j >= gap && arr[j-gap] > temp { arr[j] = arr[j-gap] j -= gap } arr[j] = temp } } } func main() { arr := []int{-3, 0, 2, -1, 5, -2} shellSort(arr) fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Shell Sort sorts in ascending order regardless of negative or positive values.
✗ Incorrect
The algorithm sorts all numbers from smallest to largest, including negatives.
🧠 Conceptual
expert1:30remaining
Time complexity of Shell Sort depends on gap sequence
Which statement about Shell Sort's time complexity is correct?
Attempts:
2 left
💡 Hint
Different gap sequences affect how quickly the array becomes sorted.
✗ Incorrect
Shell Sort's time complexity depends on the gap sequence. Some sequences give O(n^(3/2)) or better, while others degrade to O(n^2).