Skip to content

Instantly share code, notes, and snippets.

@RichardKnop
Created June 27, 2017 20:35
Show Gist options
  • Save RichardKnop/199bd69da34a5e81afe4d64c6f6a7f1d to your computer and use it in GitHub Desktop.
Save RichardKnop/199bd69da34a5e81afe4d64c6f6a7f1d to your computer and use it in GitHub Desktop.
Shellsort
package main
import (
"fmt"
)
func main() {
items := []int{4, 202, 3, 9, 6, 5, 1, 43, 506, 2, 0, 8, 7, 100, 25, 4, 5, 97, 1000, 27}
shellshort(items)
fmt.Println(items)
}
func shellshort(items []int) {
var (
n = len(items)
gaps = []int{1}
k = 1
)
for {
gap := pow(2, k) + 1
if gap > n-1 {
break
}
gaps = append([]int{gap}, gaps...)
k++
}
for _, gap := range gaps {
for i := gap; i < n; i += gap {
j := i
for j > 0 {
if items[j-gap] > items[j] {
items[j-gap], items[j] = items[j], items[j-gap]
}
j = j - gap
}
}
}
}
func pow(a, b int) int {
p := 1
for b > 0 {
if b&1 != 0 {
p *= a
}
b >>= 1
a *= a
}
return p
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment