Skip to content

Commit 322859f

Browse files
tjgurwara99github-action
and
github-action
authored
fix: [generic + sort] All ready for generics (TheAlgorithms#483)
* fix: [generic + sort] All ready for generics * GoDocMD support for Go 1.18 * Bump Go version in workflow * Updated Documentation in README.md * bump go version in GoLangCI lint action * fix: [generic + sort] review feedback * Updated Documentation in README.md * empty commit: stuck github action Co-authored-by: github-action <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 096521a commit 322859f

16 files changed

+143
-77
lines changed

.github/workflows/godocmd.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ jobs:
99
fetch-depth: 0
1010
- uses: actions/setup-go@v2
1111
with:
12-
go-version: '^1.17'
12+
go-version: '^1.18'
1313
- name: Install GoDocMD
1414
run: |
15-
go install github.com/tjgurwara99/[email protected].2
15+
go install github.com/tjgurwara99/[email protected].3
1616
- name: Configure Github Action
1717
run: |
1818
git config --global user.name github-action

.github/workflows/golang_lint_and_test.yml

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ jobs:
1515
skip: "go.mod,go.sum"
1616
- name: Setup Go
1717
uses: actions/setup-go@v2
18+
with:
19+
go-version: '^1.18'
1820
- name: Run Golang CI Lint
1921
uses: golangci/golangci-lint-action@v2
2022
with:

README.md

+17-16
Original file line numberDiff line numberDiff line change
@@ -859,22 +859,23 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
859859
---
860860
##### Functions:
861861

862-
1. [`Comb`](./sort/combSort.go#L14): No description provided.
863-
2. [`Count`](./sort/countingsort.go#L9): No description provided.
864-
3. [`Exchange`](./sort/exchangesort.go#L6): No description provided.
865-
4. [`HeapSort`](./sort/heapsort.go#L121): No description provided.
866-
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
867-
6. [`InsertionSort`](./sort/insertionsort.go#L3): No description provided.
868-
7. [`MergeIter`](./sort/mergesort.go#L51): No description provided.
869-
8. [`Mergesort`](./sort/mergesort.go#L37): Mergesort Perform mergesort on a slice of ints
870-
9. [`Partition`](./sort/quicksort.go#L10): No description provided.
871-
10. [`Pigeonhole`](./sort/pigeonholesort.go#L12): Pigeonhole sorts a slice using pigeonhole sorting algorithm.
872-
11. [`QuickSort`](./sort/quicksort.go#L37): QuickSort Sorts the entire array
873-
12. [`QuickSortRange`](./sort/quicksort.go#L24): QuickSortRange Sorts the specified range within the array
874-
13. [`RadixSort`](./sort/radixsort.go#L35): No description provided.
875-
14. [`SelectionSort`](./sort/selectionsort.go#L3): No description provided.
876-
15. [`ShellSort`](./sort/shellsort.go#L3): No description provided.
877-
16. [`SimpleSort`](./sort/simplesort.go#L11): No description provided.
862+
1. [`Bubble`](./sort/bubblesort.go#L9): Bubble is a simple generic definition of Bubble sort algorithm.
863+
2. [`Comb`](./sort/combSort.go#L17): Comb is a simple sorting algorithm which is an improvement of the bubble sorting algorithm.
864+
3. [`Count`](./sort/countingsort.go#L11): No description provided.
865+
4. [`Exchange`](./sort/exchangesort.go#L8): No description provided.
866+
5. [`HeapSort`](./sort/heapsort.go#L121): No description provided.
867+
6. [`ImprovedSimple`](./sort/simplesort.go#L27): ImprovedSimple 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
868+
7. [`Insertion`](./sort/insertionsort.go#L5): No description provided.
869+
8. [`Merge`](./sort/mergesort.go#L40): Merge Perform merge sort on a slice
870+
9. [`MergeIter`](./sort/mergesort.go#L54): No description provided.
871+
10. [`Partition`](./sort/quicksort.go#L12): No description provided.
872+
11. [`Pigeonhole`](./sort/pigeonholesort.go#L12): Pigeonhole sorts a slice using pigeonhole sorting algorithm.
873+
12. [`Quicksort`](./sort/quicksort.go#L39): Quicksort Sorts the entire array
874+
13. [`QuicksortRange`](./sort/quicksort.go#L26): QuicksortRange Sorts the specified range within the array
875+
14. [`RadixSort`](./sort/radixsort.go#L35): No description provided.
876+
15. [`Selection`](./sort/selectionsort.go#L5): No description provided.
877+
16. [`Shell`](./sort/shellsort.go#L5): No description provided.
878+
17. [`Simple`](./sort/simplesort.go#L13): No description provided.
878879

879880
---
880881
##### Types

constraints/contraints.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Package constraints has some useful generic type constraints defined which is very similar to
2+
// [golang.org/x/exp/constraints](https://pkg.go.dev/golang.org/x/exp/constraints) package.
3+
// We refrained from using that until it gets placed into the standard library - currently
4+
// there are some questions regarding this package [ref](https://github.com/golang/go/issues/50792).
5+
package constraints
6+
7+
// Signed is a generic type constraint for all signed integers.
8+
type Signed interface {
9+
~int | ~int8 | ~int16 | ~int32 | ~int64
10+
}
11+
12+
// Unsigned is a generic type constraint for all unsigned integers.
13+
type Unsigned interface {
14+
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
15+
}
16+
17+
// Integer is a generic type constraint for all integers (signed and unsigned.)
18+
type Integer interface {
19+
Signed | Unsigned
20+
}
21+
22+
// Float is a generic type constraint for all floating point types.
23+
type Float interface {
24+
~float32 | ~float64
25+
}
26+
27+
// Number is a generic type constraint for all numeric types in Go except Complex types.
28+
type Number interface {
29+
Integer | Float
30+
}
31+
32+
// Ordered is a generic type constraint for all ordered data types in Go.
33+
// Loosely speaking, in mathematics a field is an ordered field if there is a "total
34+
// order" (a binary relation - in this case `<` symbol) such that we will always have
35+
// if a < b then a + c < b + c and if 0 < a, 0 < b then 0 < a.b
36+
// The idea in Go is quite similar, though only limited to Go standard types
37+
// not user defined types.
38+
type Ordered interface {
39+
Integer | ~string | Float
40+
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/TheAlgorithms/Go
22

3-
go 1.17
3+
go 1.18

sort/bubblesort.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
package sort
55

6-
func bubbleSort(arr []int) []int {
6+
import "github.com/TheAlgorithms/Go/constraints"
7+
8+
// Bubble is a simple generic definition of Bubble sort algorithm.
9+
func Bubble[T constraints.Ordered](arr []T) []T {
710
swapped := true
811
for swapped {
912
swapped = false

sort/combSort.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
package sort
55

6+
import "github.com/TheAlgorithms/Go/constraints"
7+
68
func getNextGap(gap int) int {
79
gap = (gap * 10) / 13
810
if gap < 1 {
@@ -11,7 +13,8 @@ func getNextGap(gap int) int {
1113
return gap
1214
}
1315

14-
func Comb(data []int) []int {
16+
// Comb is a simple sorting algorithm which is an improvement of the bubble sorting algorithm.
17+
func Comb[T constraints.Ordered](data []T) []T {
1518
n := len(data)
1619
gap := n
1720
swapped := true

sort/countingsort.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
package sort
88

9-
func Count(data []int) []int {
9+
import "github.com/TheAlgorithms/Go/constraints"
10+
11+
func Count[T constraints.Number](data []int) []int {
1012
var aMin, aMax = -1000, 1000
1113
count := make([]int, aMax-aMin+1)
1214
for _, x := range data {
13-
count[x-aMin]++
15+
count[x-aMin]++ // this is the reason for having only Number constraint instead of Ordered.
1416
}
1517
z := 0
1618
for i, c := range count {

sort/exchangesort.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
package sort
55

6-
func Exchange(arr []int) []int {
6+
import "github.com/TheAlgorithms/Go/constraints"
7+
8+
func Exchange[T constraints.Ordered](arr []T) []T {
79
for i := 0; i < len(arr)-1; i++ {
810
for j := i + 1; j < len(arr); j++ {
911
if arr[i] > arr[j] {

sort/insertionsort.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package sort
22

3-
func InsertionSort(arr []int) []int {
3+
import "github.com/TheAlgorithms/Go/constraints"
4+
5+
func Insertion[T constraints.Ordered](arr []T) []T {
46
for currentIndex := 1; currentIndex < len(arr); currentIndex++ {
57
temporary := arr[currentIndex]
68
iterator := currentIndex

sort/mergesort.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package sort
22

3-
import "github.com/TheAlgorithms/Go/math/min"
3+
import (
4+
"github.com/TheAlgorithms/Go/constraints"
5+
"github.com/TheAlgorithms/Go/math/min"
6+
)
47

5-
func merge(a []int, b []int) []int {
8+
func merge[T constraints.Ordered](a []T, b []T) []T {
69

7-
var r = make([]int, len(a)+len(b))
10+
var r = make([]T, len(a)+len(b))
811
var i = 0
912
var j = 0
1013

@@ -33,22 +36,22 @@ func merge(a []int, b []int) []int {
3336

3437
}
3538

36-
//Mergesort Perform mergesort on a slice of ints
37-
func Mergesort(items []int) []int {
39+
// Merge Perform merge sort on a slice
40+
func Merge[T constraints.Ordered](items []T) []T {
3841

3942
if len(items) < 2 {
4043
return items
4144

4245
}
4346

4447
var middle = len(items) / 2
45-
var a = Mergesort(items[:middle])
46-
var b = Mergesort(items[middle:])
48+
var a = Merge(items[:middle])
49+
var b = Merge(items[middle:])
4750
return merge(a, b)
4851

4952
}
5053

51-
func MergeIter(items []int) []int {
54+
func MergeIter[T constraints.Ordered](items []T) []T {
5255
for step := 1; step < len(items); step += step {
5356
for i := 0; i+step < len(items); i += 2 * step {
5457
tmp := merge(items[i:i+step], items[i+step:min.Int(i+2*step, len(items))])

sort/quicksort.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
package sort
99

10-
func Partition(arr []int, low, high int) int {
10+
import "github.com/TheAlgorithms/Go/constraints"
11+
12+
func Partition[T constraints.Ordered](arr []T, low, high int) int {
1113
index := low - 1
1214
pivotElement := arr[high]
1315
for i := low; i < high; i++ {
@@ -20,21 +22,21 @@ func Partition(arr []int, low, high int) int {
2022
return index + 1
2123
}
2224

23-
// QuickSortRange Sorts the specified range within the array
24-
func QuickSortRange(arr []int, low, high int) {
25+
// QuicksortRange Sorts the specified range within the array
26+
func QuicksortRange[T constraints.Ordered](arr []T, low, high int) {
2527
if len(arr) <= 1 {
2628
return
2729
}
2830

2931
if low < high {
3032
pivot := Partition(arr, low, high)
31-
QuickSortRange(arr, low, pivot-1)
32-
QuickSortRange(arr, pivot+1, high)
33+
QuicksortRange(arr, low, pivot-1)
34+
QuicksortRange(arr, pivot+1, high)
3335
}
3436
}
3537

36-
// QuickSort Sorts the entire array
37-
func QuickSort(arr []int) []int {
38-
QuickSortRange(arr, 0, len(arr)-1)
38+
// Quicksort Sorts the entire array
39+
func Quicksort[T constraints.Ordered](arr []T) []T {
40+
QuicksortRange(arr, 0, len(arr)-1)
3941
return arr
4042
}

sort/selectionsort.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package sort
22

3-
func SelectionSort(arr []int) []int {
3+
import "github.com/TheAlgorithms/Go/constraints"
44

5+
func Selection[T constraints.Ordered](arr []T) []T {
56
for i := 0; i < len(arr); i++ {
67
min := i
78
for j := i + 1; j < len(arr); j++ {

sort/shellsort.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package sort
22

3-
func ShellSort(arr []int) []int {
3+
import "github.com/TheAlgorithms/Go/constraints"
4+
5+
func Shell[T constraints.Ordered](arr []T) []T {
46
for d := int(len(arr) / 2); d > 0; d /= 2 {
57
for i := d; i < len(arr); i++ {
68
for j := i; j >= d && arr[j-d] > arr[j]; j -= d {

sort/simplesort.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
package sort
1010

11-
func SimpleSort(arr []int) []int {
11+
import "github.com/TheAlgorithms/Go/constraints"
12+
13+
func Simple[T constraints.Ordered](arr []T) []T {
1214
for i := 0; i < len(arr); i++ {
1315
for j := 0; j < len(arr); j++ {
1416
if arr[i] < arr[j] {
@@ -20,9 +22,9 @@ func SimpleSort(arr []int) []int {
2022
return arr
2123
}
2224

23-
// ImprovedSimpleSort is a improve SimpleSort by skipping an unnecessary comparison of the first and last.
25+
// ImprovedSimple is a improve SimpleSort by skipping an unnecessary comparison of the first and last.
2426
// This improved version is more similar to implementation of insertion sort
25-
func ImprovedSimpleSort(arr []int) []int {
27+
func ImprovedSimple[T constraints.Ordered](arr []T) []T {
2628
for i := 1; i < len(arr); i++ {
2729
for j := 0; j < len(arr)-1; j++ {
2830
if arr[i] < arr[j] {

0 commit comments

Comments
 (0)