Skip to content

Commit

Permalink
ShellSort
Browse files Browse the repository at this point in the history
  • Loading branch information
ale8k committed Dec 9, 2021
1 parent 1754197 commit 2b2969c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Datastructures & Utility functions for GoLang
A basic GoLang module providing some datastructure implementations and utility functions for GoLang.
A basic GoLang module providing some datastructure implementations and utility functions for GoLang.

## Resources
https://www.tutorialspoint.com/data_structures_algorithms/index.htm
28 changes: 28 additions & 0 deletions slice_sorting/shell_sort/shell_sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package shellsort

func ShellSort(slice []int) {
interval := 1
inner := 0
outer := 0
insertValue := 0

for interval <= len(slice)/3 {
interval = interval*3 + 1
}

for interval > 0 {
for outer = interval; outer < len(slice); outer++ {
insertValue = slice[outer]
inner = outer

for inner > interval-1 && slice[inner-interval] >= insertValue {
slice[inner] = slice[inner-interval]
inner = inner - interval
}

slice[inner] = insertValue
}
interval = (interval - 1) / 3
}

}
33 changes: 33 additions & 0 deletions slice_sorting/shell_sort/shell_sort_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package shellsort

import (
"reflect"
"testing"
)

func TestShellSort(t *testing.T) {
type args struct {
slice []int
}
tests := []struct {
name string
args args
want []int
}{
{
name: "it sorts the slice correctly",
args: args{
slice: []int{6, 1, 8, 9},
},
want: []int{1, 6, 8, 9},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ShellSort(tt.args.slice)
if !reflect.DeepEqual(tt.want, tt.args.slice) {
t.Errorf("the sort failed, want: %v, got: %v", tt.want, tt.args.slice)
}
})
}
}

0 comments on commit 2b2969c

Please sign in to comment.