Skip to content

Commit

Permalink
fix: [generic + sort] All ready for generics (#483)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
tjgurwara99 and github-action committed Mar 26, 2022
1 parent 096521a commit 322859f
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 77 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/godocmd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
fetch-depth: 0
- uses: actions/setup-go@v2
with:
go-version: '^1.17'
go-version: '^1.18'
- name: Install GoDocMD
run: |
go install github.com/tjgurwara99/godocmd@v0.1.2
go install github.com/tjgurwara99/godocmd@v0.1.3
- name: Configure Github Action
run: |
git config --global user.name github-action
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/golang_lint_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ jobs:
skip: "go.mod,go.sum"
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: '^1.18'
- name: Run Golang CI Lint
uses: golangci/golangci-lint-action@v2
with:
Expand Down
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -859,22 +859,23 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
---
##### Functions:

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

---
##### Types
Expand Down
40 changes: 40 additions & 0 deletions constraints/contraints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Package constraints has some useful generic type constraints defined which is very similar to
// [golang.org/x/exp/constraints](https://pkg.go.dev/golang.org/x/exp/constraints) package.
// We refrained from using that until it gets placed into the standard library - currently
// there are some questions regarding this package [ref](https://github.com/golang/go/issues/50792).
package constraints

// Signed is a generic type constraint for all signed integers.
type Signed interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}

// Unsigned is a generic type constraint for all unsigned integers.
type Unsigned interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

// Integer is a generic type constraint for all integers (signed and unsigned.)
type Integer interface {
Signed | Unsigned
}

// Float is a generic type constraint for all floating point types.
type Float interface {
~float32 | ~float64
}

// Number is a generic type constraint for all numeric types in Go except Complex types.
type Number interface {
Integer | Float
}

// Ordered is a generic type constraint for all ordered data types in Go.
// Loosely speaking, in mathematics a field is an ordered field if there is a "total
// order" (a binary relation - in this case `<` symbol) such that we will always have
// if a < b then a + c < b + c and if 0 < a, 0 < b then 0 < a.b
// The idea in Go is quite similar, though only limited to Go standard types
// not user defined types.
type Ordered interface {
Integer | ~string | Float
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/TheAlgorithms/Go

go 1.17
go 1.18
5 changes: 4 additions & 1 deletion sort/bubblesort.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

package sort

func bubbleSort(arr []int) []int {
import "github.com/TheAlgorithms/Go/constraints"

// Bubble is a simple generic definition of Bubble sort algorithm.
func Bubble[T constraints.Ordered](arr []T) []T {
swapped := true
for swapped {
swapped = false
Expand Down
5 changes: 4 additions & 1 deletion sort/combSort.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package sort

import "github.com/TheAlgorithms/Go/constraints"

func getNextGap(gap int) int {
gap = (gap * 10) / 13
if gap < 1 {
Expand All @@ -11,7 +13,8 @@ func getNextGap(gap int) int {
return gap
}

func Comb(data []int) []int {
// Comb is a simple sorting algorithm which is an improvement of the bubble sorting algorithm.
func Comb[T constraints.Ordered](data []T) []T {
n := len(data)
gap := n
swapped := true
Expand Down
6 changes: 4 additions & 2 deletions sort/countingsort.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

package sort

func Count(data []int) []int {
import "github.com/TheAlgorithms/Go/constraints"

func Count[T constraints.Number](data []int) []int {
var aMin, aMax = -1000, 1000
count := make([]int, aMax-aMin+1)
for _, x := range data {
count[x-aMin]++
count[x-aMin]++ // this is the reason for having only Number constraint instead of Ordered.
}
z := 0
for i, c := range count {
Expand Down
4 changes: 3 additions & 1 deletion sort/exchangesort.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

package sort

func Exchange(arr []int) []int {
import "github.com/TheAlgorithms/Go/constraints"

func Exchange[T constraints.Ordered](arr []T) []T {
for i := 0; i < len(arr)-1; i++ {
for j := i + 1; j < len(arr); j++ {
if arr[i] > arr[j] {
Expand Down
4 changes: 3 additions & 1 deletion sort/insertionsort.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package sort

func InsertionSort(arr []int) []int {
import "github.com/TheAlgorithms/Go/constraints"

func Insertion[T constraints.Ordered](arr []T) []T {
for currentIndex := 1; currentIndex < len(arr); currentIndex++ {
temporary := arr[currentIndex]
iterator := currentIndex
Expand Down
19 changes: 11 additions & 8 deletions sort/mergesort.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package sort

import "github.com/TheAlgorithms/Go/math/min"
import (
"github.com/TheAlgorithms/Go/constraints"
"github.com/TheAlgorithms/Go/math/min"
)

func merge(a []int, b []int) []int {
func merge[T constraints.Ordered](a []T, b []T) []T {

var r = make([]int, len(a)+len(b))
var r = make([]T, len(a)+len(b))
var i = 0
var j = 0

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

}

//Mergesort Perform mergesort on a slice of ints
func Mergesort(items []int) []int {
// Merge Perform merge sort on a slice
func Merge[T constraints.Ordered](items []T) []T {

if len(items) < 2 {
return items

}

var middle = len(items) / 2
var a = Mergesort(items[:middle])
var b = Mergesort(items[middle:])
var a = Merge(items[:middle])
var b = Merge(items[middle:])
return merge(a, b)

}

func MergeIter(items []int) []int {
func MergeIter[T constraints.Ordered](items []T) []T {
for step := 1; step < len(items); step += step {
for i := 0; i+step < len(items); i += 2 * step {
tmp := merge(items[i:i+step], items[i+step:min.Int(i+2*step, len(items))])
Expand Down
18 changes: 10 additions & 8 deletions sort/quicksort.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

package sort

func Partition(arr []int, low, high int) int {
import "github.com/TheAlgorithms/Go/constraints"

func Partition[T constraints.Ordered](arr []T, low, high int) int {
index := low - 1
pivotElement := arr[high]
for i := low; i < high; i++ {
Expand All @@ -20,21 +22,21 @@ func Partition(arr []int, low, high int) int {
return index + 1
}

// QuickSortRange Sorts the specified range within the array
func QuickSortRange(arr []int, low, high int) {
// QuicksortRange Sorts the specified range within the array
func QuicksortRange[T constraints.Ordered](arr []T, low, high int) {
if len(arr) <= 1 {
return
}

if low < high {
pivot := Partition(arr, low, high)
QuickSortRange(arr, low, pivot-1)
QuickSortRange(arr, pivot+1, high)
QuicksortRange(arr, low, pivot-1)
QuicksortRange(arr, pivot+1, high)
}
}

// QuickSort Sorts the entire array
func QuickSort(arr []int) []int {
QuickSortRange(arr, 0, len(arr)-1)
// Quicksort Sorts the entire array
func Quicksort[T constraints.Ordered](arr []T) []T {
QuicksortRange(arr, 0, len(arr)-1)
return arr
}
3 changes: 2 additions & 1 deletion sort/selectionsort.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package sort

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

func Selection[T constraints.Ordered](arr []T) []T {
for i := 0; i < len(arr); i++ {
min := i
for j := i + 1; j < len(arr); j++ {
Expand Down
4 changes: 3 additions & 1 deletion sort/shellsort.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package sort

func ShellSort(arr []int) []int {
import "github.com/TheAlgorithms/Go/constraints"

func Shell[T constraints.Ordered](arr []T) []T {
for d := int(len(arr) / 2); d > 0; d /= 2 {
for i := d; i < len(arr); i++ {
for j := i; j >= d && arr[j-d] > arr[j]; j -= d {
Expand Down
8 changes: 5 additions & 3 deletions sort/simplesort.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

package sort

func SimpleSort(arr []int) []int {
import "github.com/TheAlgorithms/Go/constraints"

func Simple[T constraints.Ordered](arr []T) []T {
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr); j++ {
if arr[i] < arr[j] {
Expand All @@ -20,9 +22,9 @@ func SimpleSort(arr []int) []int {
return arr
}

// ImprovedSimpleSort is a improve SimpleSort by skipping an unnecessary comparison of the first and last.
// 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
func ImprovedSimpleSort(arr []int) []int {
func ImprovedSimple[T constraints.Ordered](arr []T) []T {
for i := 1; i < len(arr); i++ {
for j := 0; j < len(arr)-1; j++ {
if arr[i] < arr[j] {
Expand Down
Loading

0 comments on commit 322859f

Please sign in to comment.