0
0
DSA Goprogramming~20 mins

Shell Sort Algorithm in DSA Go - 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 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)
}
A[54 34 23 12 8 3 2 1]
B[1 3 2 8 12 23 34 54]
C[23 12 1 8 34 54 2 3]
D[1 2 3 8 12 23 34 54]
Attempts:
2 left
💡 Hint
Shell Sort sorts by comparing elements far apart and gradually reducing the gap.
🧠 Conceptual
intermediate
1:30remaining
Gap sequence in Shell Sort
Which of the following gap sequences is used in the provided Shell Sort implementation?
AStart with 1 and multiply by 2 each time
BUse prime numbers as gaps
CStart with n/2 and keep dividing by 2 until gap is 0
DUse Fibonacci numbers as gaps
Attempts:
2 left
💡 Hint
Look at how the gap variable changes in the code.
🔧 Debug
advanced
2: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)
}
AIndex out of range runtime panic
B[2 3 5 6 8]
C[5 3 8 6 2]
DCompilation error
Attempts:
2 left
💡 Hint
Check the condition in the inner loop comparing j and gap.
Predict Output
advanced
2: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)
}
A[-3 -2 -1 0 2 5]
B[-3 -1 -2 0 2 5]
C[5 2 0 -1 -2 -3]
D[-2 -3 -1 0 2 5]
Attempts:
2 left
💡 Hint
Shell Sort sorts in ascending order regardless of negative or positive values.
🧠 Conceptual
expert
1:30remaining
Time complexity of Shell Sort depends on gap sequence
Which statement about Shell Sort's time complexity is correct?
ATime complexity is always O(n log n) regardless of gap sequence
BTime complexity can vary from O(n^2) to O(n^(3/2)) depending on gap sequence
CTime complexity is always O(n^2) for any gap sequence
DTime complexity is O(n) if the array is already sorted
Attempts:
2 left
💡 Hint
Different gap sequences affect how quickly the array becomes sorted.