diff --git a/README.md b/README.md index 451f2e7..f793a60 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # Datastructures & Utility functions for GoLang -A basic GoLang module providing some datastructure implementations and utility functions for GoLang. \ No newline at end of file +A basic GoLang module providing some datastructure implementations and utility functions for GoLang. + +## Resources +https://www.tutorialspoint.com/data_structures_algorithms/index.htm \ No newline at end of file diff --git a/slice_sorting/shell_sort/shell_sort.go b/slice_sorting/shell_sort/shell_sort.go new file mode 100644 index 0000000..a0bf981 --- /dev/null +++ b/slice_sorting/shell_sort/shell_sort.go @@ -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 + } + +} diff --git a/slice_sorting/shell_sort/shell_sort_test.go b/slice_sorting/shell_sort/shell_sort_test.go new file mode 100644 index 0000000..220762a --- /dev/null +++ b/slice_sorting/shell_sort/shell_sort_test.go @@ -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) + } + }) + } +}