0
0
DSA Goprogramming

Shell Sort Algorithm in DSA Go

Choose your learning style9 modes available
Mental Model
Shell sort improves simple sorting by comparing elements far apart first, then reducing the gap to sort closer elements, making the list more ordered step by step.
Analogy: Imagine organizing books on a shelf by first sorting every 4th book, then every 2nd book, and finally sorting all books one by one. This way, big mistakes get fixed early, making the final sorting easier.
Array: [8, 5, 3, 7, 6, 2, 4, 1]
Gap: 4
Indexes compared: 0 and 4, 1 and 5, 2 and 6, 3 and 7
Dry Run Walkthrough
Input: array: [8, 5, 3, 7, 6, 2, 4, 1]
Goal: Sort the array in ascending order using Shell sort
Step 1: Start with gap = 4, compare and sort elements 4 apart
[6, 2, 3, 1, 8, 5, 4, 7]
Why: Sorting elements far apart reduces large disorder early
Step 2: Reduce gap to 2, perform insertion sort on subarrays separated by gap 2
[3, 1, 4, 2, 6, 5, 7, 8]
Why: Closer elements are sorted to improve order further
Step 3: Reduce gap to 1, perform final insertion sort on entire array
[1, 2, 3, 4, 5, 6, 7, 8]
Why: Final pass ensures full sorted order
Result:
[1, 2, 3, 4, 5, 6, 7, 8]
Annotated Code
DSA Go
package main

import "fmt"

func shellSort(arr []int) {
	n := len(arr)
	for gap := n / 2; gap > 0; gap /= 2 {
		// Perform gapped insertion sort for this gap size
		for i := gap; i < n; i++ {
			// Save current element to insert it later
			temp := arr[i]
			j := i
			// Shift earlier gap-sorted elements up until correct location found
			for j >= gap && arr[j-gap] > temp {
				arr[j] = arr[j-gap]
				j -= gap
			}
			// Place temp at its correct position
			arr[j] = temp
		}
	}
}

func main() {
	arr := []int{8, 5, 3, 7, 6, 2, 4, 1}
	shellSort(arr)
	fmt.Println(arr)
}
for gap := n / 2; gap > 0; gap /= 2 {
reduce gap size step by step to sort elements closer and closer
for i := gap; i < n; i++ {
iterate over elements starting from current gap index
temp := arr[i]
store current element to insert it in correct position
for j >= gap && arr[j-gap] > temp {
shift elements that are greater than temp to the right
arr[j] = temp
insert temp at its correct sorted position
OutputSuccess
[1 2 3 4 5 6 7 8]
Complexity Analysis
Time: O(n^(3/2)) on average because gap sequence reduces disorder efficiently but still requires multiple passes
Space: O(1) because sorting is done in place without extra arrays
vs Alternative: Faster than simple insertion sort (O(n^2)) for larger arrays because it reduces disorder early with bigger gaps
Edge Cases
empty array
function returns immediately without errors
DSA Go
for gap := n / 2; gap > 0; gap /= 2 {
array with one element
array remains unchanged as it is already sorted
DSA Go
for gap := n / 2; gap > 0; gap /= 2 {
array with all elements equal
no swaps occur, array remains the same
DSA Go
for j >= gap && arr[j-gap] > temp {
When to Use This Pattern
When you see sorting problems where simple insertion sort is too slow, and you want a faster in-place method without extra memory, reach for Shell sort because it improves insertion sort by sorting elements far apart first.
Common Mistakes
Mistake: Not reducing the gap correctly by dividing by 2 each time
Fix: Use gap /= 2 in the loop to reduce gap size stepwise
Mistake: Not shifting elements properly inside the inner loop
Fix: Shift elements greater than temp to the right before inserting temp
Summary
Shell sort sorts an array by comparing elements far apart and gradually reducing the gap to fully sort the array.
Use Shell sort when you want a faster in-place sorting than insertion sort for medium-sized arrays.
The key insight is sorting elements far apart first reduces disorder quickly, making final sorting easier.