diff --git a/go-dsa/sorting/shell_sort.go b/go-dsa/sorting/shell_sort.go new file mode 100644 index 0000000..ef1f545 --- /dev/null +++ b/go-dsa/sorting/shell_sort.go @@ -0,0 +1,27 @@ +package sorting + +type ShellSort struct{} + +func (i *ShellSort) Sort(arr []int) { + // 1. start with a large gap, then reduce the gap until it becomes 1 + // 2. perform insertion sort on the elements with the gap + + for gap := len(arr) / 2; gap > 0; gap /= 2 { + // start from the gap element and move to the right + for i := gap; i < len(arr); i++ { + // store the current element + temp := arr[i] + j := i + + // run the insertion sort on the elements with the gap + // shift the elements to the right until the correct position is found + for j >= gap && arr[j-gap] > temp { + arr[j] = arr[j-gap] + j -= gap + } + + // insert the temp element at the correct position + arr[j] = temp + } + } +} diff --git a/go-dsa/sorting/sorting_test.go b/go-dsa/sorting/sorting_test.go index b4a0283..618c541 100644 --- a/go-dsa/sorting/sorting_test.go +++ b/go-dsa/sorting/sorting_test.go @@ -15,6 +15,7 @@ func TestSorting(t *testing.T) { algorithms := []Sorting{ &InsertionSort{}, &HeapSort{}, + &ShellSort{}, } tests := buildTestData()