diff --git a/README.md b/README.md index a6d6afd99..a5c904b18 100644 --- a/README.md +++ b/README.md @@ -758,19 +758,20 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute. --- ##### Functions: -1. [`Count`](./sort/countingsort.go#L9): No description provided. -2. [`Exchange`](./sort/exchangesort.go#L6): No description provided. -3. [`HeapSort`](./sort/heapsort.go#L121): No description provided. -4. [`ImprovedSimpleSort`](./sort/simplesort.go#L25): ImprovedSimpleSort is a improve SimpleSort by skipping an unnecessary comparison of the first and last. This improved version is more similar to implementation of insertion sort -5. [`InsertionSort`](./sort/insertionsort.go#L3): No description provided. -6. [`Mergesort`](./sort/mergesort.go#L35): Mergesort Perform mergesort on a slice of ints -7. [`Pigeonhole`](./sort/pigeonholesort.go#L12): Pigeonhole sorts a slice using pigeonhole sorting algorithm. -8. [`QuickSort`](./sort/quicksort.go#L37): QuickSort Sorts the entire array -9. [`QuickSortRange`](./sort/quicksort.go#L24): QuickSortRange Sorts the specified range within the array -10. [`RadixSort`](./sort/radixsort.go#L35): No description provided. -11. [`SelectionSort`](./sort/selectionsort.go#L3): No description provided. -12. [`ShellSort`](./sort/shellsort.go#L3): No description provided. -13. [`SimpleSort`](./sort/simplesort.go#L11): No description provided. +1. [`Comb`](./sort/combSort.go#L14): No description provided. +2. [`Count`](./sort/countingsort.go#L9): No description provided. +3. [`Exchange`](./sort/exchangesort.go#L6): No description provided. +4. [`HeapSort`](./sort/heapsort.go#L121): No description provided. +5. [`ImprovedSimpleSort`](./sort/simplesort.go#L25): ImprovedSimpleSort is a improve SimpleSort by skipping an unnecessary comparison of the first and last. This improved version is more similar to implementation of insertion sort +6. [`InsertionSort`](./sort/insertionsort.go#L3): No description provided. +7. [`Mergesort`](./sort/mergesort.go#L35): Mergesort Perform mergesort on a slice of ints +8. [`Pigeonhole`](./sort/pigeonholesort.go#L12): Pigeonhole sorts a slice using pigeonhole sorting algorithm. +9. [`QuickSort`](./sort/quicksort.go#L37): QuickSort Sorts the entire array +10. [`QuickSortRange`](./sort/quicksort.go#L24): QuickSortRange Sorts the specified range within the array +11. [`RadixSort`](./sort/radixsort.go#L35): No description provided. +12. [`SelectionSort`](./sort/selectionsort.go#L3): No description provided. +13. [`ShellSort`](./sort/shellsort.go#L3): No description provided. +14. [`SimpleSort`](./sort/simplesort.go#L11): No description provided. --- ##### Types diff --git a/sort/combSort.go b/sort/combSort.go new file mode 100644 index 000000000..fa7b173d9 --- /dev/null +++ b/sort/combSort.go @@ -0,0 +1,30 @@ +// Implementation of comb sort algorithm, an improvement of bubble sort +// Reference: https://www.geeksforgeeks.org/comb-sort/ + +package sort + +func getNextGap(gap int) int { + gap = (gap * 10) / 13 + if gap < 1 { + return 1 + } + return gap +} + +func Comb(data []int) []int { + n := len(data) + gap := n + swapped := true + + for gap != 1 || swapped { + gap = getNextGap(gap) + swapped = false + for i := 0; i < n-gap; i++ { + if data[i] > data[i+gap] { + data[i], data[i+gap] = data[i+gap], data[i] + swapped = true + } + } + } + return data +} diff --git a/sort/sorts_test.go b/sort/sorts_test.go index 5342121cc..be239205e 100644 --- a/sort/sorts_test.go +++ b/sort/sorts_test.go @@ -122,6 +122,10 @@ func TestSelection(t *testing.T) { testFramework(t, SelectionSort) } +func TestComb(t *testing.T) { + testFramework(t, Comb) +} + func TestPigeonhole(t *testing.T) { testFramework(t, Pigeonhole) } @@ -216,6 +220,10 @@ func BenchmarkSelection(b *testing.B) { benchmarkFramework(b, SelectionSort) } +func BenchmarkComb(b *testing.B) { + benchmarkFramework(b, Comb) +} + func BenchmarkPigeonhole(b *testing.B) { benchmarkFramework(b, Pigeonhole) }