Skip to content

Commit

Permalink
Merge pull request #18 from osspkg/dev
Browse files Browse the repository at this point in the history
add base sorting algorithm
  • Loading branch information
markus621 committed Jun 19, 2024
2 parents a209874 + 0d428f3 commit 32707e5
Show file tree
Hide file tree
Showing 22 changed files with 534 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.18', '1.19', '1.20', '1.21' ]
go: [ '1.20', '1.21' ]
steps:
- uses: actions/checkout@v3

Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -371,5 +371,5 @@ linters:
- exportloopref
- gci
- gosec
- lll
# - lll
fast: false
1 change: 1 addition & 0 deletions .gvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.20.14
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2019-2023, Mikhail Knyazhev <[email protected]>
Copyright (c) 2019-2024, Mikhail Knyazhev <[email protected]>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ go get -u go.osspkg.com/algorithms

## List

- Topological sorting
- [Kahn's Algorithm](graph/kahn/)
- Reducing numbers
- [Shorten](shorten/)
- Graph
- Topological sorting
- [Kahn's Algorithm](graph/kahn/type.go)
- Information compression
- [Reducing numbers](shorten/shorten.go)
- Sorting algorithm
- [Bubble sort](sorts/bubble.go)
- [Cocktail shaker sort](sorts/cocktail.go)
- [Insertion sort](sorts/insertion.go)
- [Merge sort](sorts/merge.go)
- [Selection sort](sorts/selection.go)

## License

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module go.osspkg.com/algorithms

go 1.18
go 1.20

require github.com/stretchr/testify v1.8.4
require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
2 changes: 1 addition & 1 deletion graph/kahn/type.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2023 Mikhail Knyazhev <[email protected]>. All rights reserved.
* Copyright (c) 2019-2024 Mikhail Knyazhev <[email protected]>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

Expand Down
2 changes: 1 addition & 1 deletion graph/kahn/type_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2023 Mikhail Knyazhev <[email protected]>. All rights reserved.
* Copyright (c) 2019-2024 Mikhail Knyazhev <[email protected]>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

Expand Down
2 changes: 1 addition & 1 deletion shorten/shorten.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2023 Mikhail Knyazhev <[email protected]>. All rights reserved.
* Copyright (c) 2019-2024 Mikhail Knyazhev <[email protected]>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

Expand Down
2 changes: 1 addition & 1 deletion shorten/shorten_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2023 Mikhail Knyazhev <[email protected]>. All rights reserved.
* Copyright (c) 2019-2024 Mikhail Knyazhev <[email protected]>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

Expand Down
26 changes: 26 additions & 0 deletions sorts/bubble.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2019-2024 Mikhail Knyazhev <[email protected]>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

// see: https://en.wikipedia.org/wiki/Bubble_sort

package sorts

func Bubble[T any](list []T, less func(i, j int) bool) {
var (
changed bool
)
for j := 0; j < len(list)-1; j++ {
changed = false
for i := 0; i < len(list)-1-j; i++ {
if less(i+1, i) {
list[i], list[i+1] = list[i+1], list[i]
changed = true
}
}
if !changed {
return
}
}
}
60 changes: 60 additions & 0 deletions sorts/bubble_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2019-2024 Mikhail Knyazhev <[email protected]>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

package sorts_test

import (
"testing"

"github.com/stretchr/testify/require"
"go.osspkg.com/algorithms/sorts"
)

func TestUnit_SortBubble(t *testing.T) {
tests := []struct {
name string
args []int
want []int
}{
{
name: "IntCase1",
args: nil,
want: nil,
},
{
name: "IntCase2",
args: []int{1, 67, 23, 1, 5, 9, 5, 32, 1, 34, 68, 9, 5, 23, 0, 0, 0, 0, 0, 5, 5, 3, 2, 1},
want: []int{0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 3, 5, 5, 5, 5, 5, 9, 9, 23, 23, 32, 34, 67, 68},
},
{
name: "IntCase3",
args: []int{1},
want: []int{1},
},
{
name: "IntCase4",
args: []int{9, 4, 1, 6, 0},
want: []int{0, 1, 4, 6, 9},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sorts.Bubble(tt.args, func(i, j int) bool {
return tt.args[i] < tt.args[j]
})
require.Equal(t, tt.want, tt.args)
})
}
}

func Benchmark_SortBubble(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
arr := []int{30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
sorts.Bubble(arr, func(i, j int) bool {
return arr[i] < arr[j]
})
}
}
37 changes: 37 additions & 0 deletions sorts/cocktail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2019-2024 Mikhail Knyazhev <[email protected]>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

// see: https://en.wikipedia.org/wiki/Cocktail_shaker_sort

package sorts

func Cocktail[T any](list []T, less func(i, j int) bool) {
changed, toRight := false, true
min, max := 0, len(list)-1
for {
changed = false
if toRight {
for i := min; i < max; i++ {
if less(i+1, i) {
list[i], list[i+1] = list[i+1], list[i]
changed = true
}
}
max--
} else {
for i := max; i >= min; i-- {
if less(i+1, i) {
list[i], list[i+1] = list[i+1], list[i]
changed = true
}
}
min++
}
toRight = !toRight
if !changed || min >= max {
return
}
}
}
65 changes: 65 additions & 0 deletions sorts/cocktail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2019-2024 Mikhail Knyazhev <[email protected]>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

package sorts_test

import (
"testing"

"github.com/stretchr/testify/require"
"go.osspkg.com/algorithms/sorts"
)

func TestUnit_SortCocktail(t *testing.T) {
tests := []struct {
name string
args []int
want []int
}{
{
name: "IntCase1",
args: nil,
want: nil,
},
{
name: "IntCase2",
args: []int{1, 67, 23, 1, 5, 9, 5, 32, 1, 34, 68, 9, 5, 23, 0, 0, 0, 0, 0, 5, 5, 3, 2, 1},
want: []int{0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 3, 5, 5, 5, 5, 5, 9, 9, 23, 23, 32, 34, 67, 68},
},
{
name: "IntCase3",
args: []int{1},
want: []int{1},
},
{
name: "IntCase4",
args: []int{4, 3, 2, 1, 0},
want: []int{0, 1, 2, 3, 4},
},
{
name: "IntCase5",
args: []int{3, 2, 1, 0},
want: []int{0, 1, 2, 3},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sorts.Cocktail(tt.args, func(i, j int) bool {
return tt.args[i] < tt.args[j]
})
require.Equal(t, tt.want, tt.args)
})
}
}

func Benchmark_SortCocktail(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
arr := []int{30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
sorts.Cocktail(arr, func(i, j int) bool {
return arr[i] < arr[j]
})
}
}
21 changes: 21 additions & 0 deletions sorts/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2019-2024 Mikhail Knyazhev <[email protected]>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

package sorts

import (
"sort"
"testing"
)

func Benchmark_Default_SortSlice(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
arr := []int{30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
sort.Slice(arr, func(i, j int) bool {
return arr[i] < arr[j]
})
}
}
22 changes: 22 additions & 0 deletions sorts/insertion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2019-2024 Mikhail Knyazhev <[email protected]>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

// see: https://en.wikipedia.org/wiki/Insertion_sort

package sorts

func Insertion[T any](list []T, less func(i, j int) bool) {
if len(list) < 2 {
return
}
for el := 1; el < len(list); el++ {
for i := el; i > 0; i-- {
if less(i-1, i) {
break
}
list[i-1], list[i] = list[i], list[i-1]
}
}
}
Loading

0 comments on commit 32707e5

Please sign in to comment.