Skip to content

Commit f53ca7b

Browse files
committed
add Comb Sort
1 parent f68db69 commit f53ca7b

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ Algorithms covered so far:
202202
| [Odd-Even Sort](#odd-even-sort) | OddEvenSort |
203203
| [Odd-Even Merge Sort](#odd-even-merge-sort) | OddEvenMergeSort |
204204
| [Odd-Even Merge Sort (parallel)](#odd-even-merge-sort) | ParallelOddEvenMergeSort |
205+
| [Comb Sort](#comb-sort) | CombSort |
205206

206207

207208
<br />
@@ -591,4 +592,22 @@ where n is the number of items to be sorted.
591592
| Type | Worst-Case | Average-Case | Best-Case | in-place | stable | Space Complexity |
592593
| :-: | :-: | :-: | :-: | :-: | :-: | :-: |
593594
| non-parallel | ![O(nlog^2_n)](https://latex.codecogs.com/svg.image?O(n\log^2&space;n)) | ![O(nlog^2_n)](https://latex.codecogs.com/svg.image?O(n\log^2&space;n)) | ![O(nlog^2_n)](https://latex.codecogs.com/svg.image?O(n\log^2&space;n)) | Yes | yes | total : ![O(nlog^2_n)](https://latex.codecogs.com/svg.image?O(n\log^2&space;n)), auxiliary : ![O(nlog^2_n)](https://latex.codecogs.com/svg.image?O(n\log^2&space;n)) |
594-
| parallel | ![O(log^2_n)](https://latex.codecogs.com/svg.image?O(\log^2&space;n)) | ![O(log^2_n)](https://latex.codecogs.com/svg.image?O(\log^2&space;n)) | ![O(log^2_n)](https://latex.codecogs.com/svg.image?O(\log^2&space;n)) | Yes | yes | total : ![O(nlog^2_n)](https://latex.codecogs.com/svg.image?O(n\log^2&space;n)), auxiliary : ![O(nlog^2_n)](https://latex.codecogs.com/svg.image?O(n\log^2&space;n)) |
595+
| parallel | ![O(log^2_n)](https://latex.codecogs.com/svg.image?O(\log^2&space;n)) | ![O(log^2_n)](https://latex.codecogs.com/svg.image?O(\log^2&space;n)) | ![O(log^2_n)](https://latex.codecogs.com/svg.image?O(\log^2&space;n)) | Yes | yes | total : ![O(nlog^2_n)](https://latex.codecogs.com/svg.image?O(n\log^2&space;n)), auxiliary : ![O(nlog^2_n)](https://latex.codecogs.com/svg.image?O(n\log^2&space;n)) |
596+
597+
598+
599+
<br />
600+
<br />
601+
602+
## Comb Sort
603+
604+
<br />
605+
Comb sort is a relatively simple sorting algorithm originally designed by Włodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered (and given the name "Combsort") by Stephen Lacey and Richard Box in 1991. Comb sort improves on bubble sort in the same way that Shellsort improves on insertion sort.
606+
<br />
607+
608+
### COMPLEXITY
609+
610+
611+
| Worst-Case | Average-Case | Best-Case | in-place | stable | Space Complexity |
612+
| :-: | :-: | :-: | :-: | :-: | :-: |
613+
| ![O(n^2)](https://latex.codecogs.com/svg.image?O(n^{2})) | ![O(n^2/p^2)](https://latex.codecogs.com/svg.image?O(n^{2}/2^{p})) | ![O(nlog_n)](https://latex.codecogs.com/svg.image?O(n\log&space;n)) | Yes | No | total : ![O(n)](https://latex.codecogs.com/svg.image?O(n)), auxiliary : ![O(1)](https://latex.codecogs.com/svg.image?O(1)) |

simplytest/base_sort_list.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,17 @@ func CallParallelOddEvenMergeSort(origin []int, verify []int, callName string) O
404404
}
405405
return OutputForm{false, callName, -1, false, ""}
406406
}
407+
408+
func CallCombSort(origin []int, verify []int, callName string) OutputForm {
409+
if COMB_SORT {
410+
test := make([]int, len(origin))
411+
copy(test, origin)
412+
fmt.Printf("runing %s...\n", callName)
413+
start := time.Now()
414+
sorts.CombSort(test)
415+
end := time.Since(start)
416+
eq, err := Equal(verify, test)
417+
return OutputForm{true, callName, end.Nanoseconds(), eq, err}
418+
}
419+
return OutputForm{false, callName, -1, false, ""}
420+
}

simplytest/option.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const (
3939
ODDEVEN_SORT Activate = true
4040
ODDEVEN_MERGE_SORT Activate = true // The length n of the array must be a power of 2
4141
PARALLEL_ODDEVEN_MERGE_SORT Activate = true // The length n of the array must be a power of 2
42+
COMB_SORT Activate = true
4243
)
4344

4445
// Section 2.

simplytest/test_run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func callSortTest(origin, verify []int) {
5454
q = append(q, CallOddEvenSort(origin, verify, "odd-even sort"))
5555
q = append(q, CallOddEvenMergeSort(origin, verify, "odd-even merge sort"))
5656
q = append(q, CallParallelOddEvenMergeSort(origin, verify, "parallel odd-even merge sort"))
57+
q = append(q, CallCombSort(origin, verify, "comb sort"))
5758
var pf string = ""
5859

5960
pf += fmt.Sprintf("\n+%s+\n", strings.Repeat("-", 97))

sorts/comb_sort.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
author : kdgyun
3+
4+
link : https://st-lab.tistory.com
5+
link : https://github.com/kdgyun
6+
*/
7+
8+
package sorts
9+
10+
func CombSort(a []int) {
11+
combSort(a, 0, len(a))
12+
}
13+
14+
func combSort(a []int, lo, hi int) {
15+
gap := hi - lo
16+
shrink := 1.3
17+
sorted := false
18+
19+
for !sorted {
20+
gap = int(float64(gap) / (shrink))
21+
if gap <= 1 {
22+
sorted = true
23+
gap = 1
24+
}
25+
26+
for i := lo; i < hi-gap; i++ {
27+
if a[i] > a[i+gap] {
28+
a[i], a[i+gap] = a[i+gap], a[i]
29+
sorted = false
30+
}
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)