Skip to content

Comb Sort algorithm implementation #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions sort/combSort.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 8 additions & 0 deletions sort/sorts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}