Skip to content

Commit

Permalink
add Comb Sort
Browse files Browse the repository at this point in the history
  • Loading branch information
kdgyun committed Mar 7, 2022
1 parent f68db69 commit f53ca7b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ Algorithms covered so far:
| [Odd-Even Sort](#odd-even-sort) | OddEvenSort |
| [Odd-Even Merge Sort](#odd-even-merge-sort) | OddEvenMergeSort |
| [Odd-Even Merge Sort (parallel)](#odd-even-merge-sort) | ParallelOddEvenMergeSort |
| [Comb Sort](#comb-sort) | CombSort |


<br />
Expand Down Expand Up @@ -591,4 +592,22 @@ where n is the number of items to be sorted.
| Type | Worst-Case | Average-Case | Best-Case | in-place | stable | Space Complexity |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| 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)) |
| 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)) |
| 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)) |



<br />
<br />

## Comb Sort

<br />
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.
<br />

### COMPLEXITY


| Worst-Case | Average-Case | Best-Case | in-place | stable | Space Complexity |
| :-: | :-: | :-: | :-: | :-: | :-: |
| ![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)) |
14 changes: 14 additions & 0 deletions simplytest/base_sort_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,17 @@ func CallParallelOddEvenMergeSort(origin []int, verify []int, callName string) O
}
return OutputForm{false, callName, -1, false, ""}
}

func CallCombSort(origin []int, verify []int, callName string) OutputForm {
if COMB_SORT {
test := make([]int, len(origin))
copy(test, origin)
fmt.Printf("runing %s...\n", callName)
start := time.Now()
sorts.CombSort(test)
end := time.Since(start)
eq, err := Equal(verify, test)
return OutputForm{true, callName, end.Nanoseconds(), eq, err}
}
return OutputForm{false, callName, -1, false, ""}
}
1 change: 1 addition & 0 deletions simplytest/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
ODDEVEN_SORT Activate = true
ODDEVEN_MERGE_SORT Activate = true // The length n of the array must be a power of 2
PARALLEL_ODDEVEN_MERGE_SORT Activate = true // The length n of the array must be a power of 2
COMB_SORT Activate = true
)

// Section 2.
Expand Down
1 change: 1 addition & 0 deletions simplytest/test_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func callSortTest(origin, verify []int) {
q = append(q, CallOddEvenSort(origin, verify, "odd-even sort"))
q = append(q, CallOddEvenMergeSort(origin, verify, "odd-even merge sort"))
q = append(q, CallParallelOddEvenMergeSort(origin, verify, "parallel odd-even merge sort"))
q = append(q, CallCombSort(origin, verify, "comb sort"))
var pf string = ""

pf += fmt.Sprintf("\n+%s+\n", strings.Repeat("-", 97))
Expand Down
33 changes: 33 additions & 0 deletions sorts/comb_sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
author : kdgyun
link : https://st-lab.tistory.com
link : https://github.com/kdgyun
*/

package sorts

func CombSort(a []int) {
combSort(a, 0, len(a))
}

func combSort(a []int, lo, hi int) {
gap := hi - lo
shrink := 1.3
sorted := false

for !sorted {
gap = int(float64(gap) / (shrink))
if gap <= 1 {
sorted = true
gap = 1
}

for i := lo; i < hi-gap; i++ {
if a[i] > a[i+gap] {
a[i], a[i+gap] = a[i+gap], a[i]
sorted = false
}
}
}
}

0 comments on commit f53ca7b

Please sign in to comment.