From 0d428f37ea3f894c9f9b9e8c047d4abc14ebb785 Mon Sep 17 00:00:00 2001 From: Mikhail Knyazhev Date: Tue, 18 Jun 2024 15:47:10 +0300 Subject: [PATCH] add base sorting algorithm --- .github/workflows/ci.yml | 2 +- .golangci.yml | 2 +- .gvmrc | 1 + LICENSE | 2 +- README.md | 15 +++++++--- go.mod | 4 +-- go.sum | 4 +-- graph/kahn/type.go | 2 +- graph/kahn/type_test.go | 2 +- shorten/shorten.go | 2 +- shorten/shorten_test.go | 2 +- sorts/bubble.go | 26 ++++++++++++++++ sorts/bubble_test.go | 60 +++++++++++++++++++++++++++++++++++++ sorts/cocktail.go | 37 +++++++++++++++++++++++ sorts/cocktail_test.go | 65 ++++++++++++++++++++++++++++++++++++++++ sorts/common_test.go | 21 +++++++++++++ sorts/insertion.go | 22 ++++++++++++++ sorts/insertion_test.go | 65 ++++++++++++++++++++++++++++++++++++++++ sorts/merge.go | 62 ++++++++++++++++++++++++++++++++++++++ sorts/merge_test.go | 65 ++++++++++++++++++++++++++++++++++++++++ sorts/selection.go | 23 ++++++++++++++ sorts/selection_test.go | 65 ++++++++++++++++++++++++++++++++++++++++ 22 files changed, 534 insertions(+), 15 deletions(-) create mode 100644 .gvmrc create mode 100644 sorts/bubble.go create mode 100644 sorts/bubble_test.go create mode 100644 sorts/cocktail.go create mode 100644 sorts/cocktail_test.go create mode 100644 sorts/common_test.go create mode 100644 sorts/insertion.go create mode 100644 sorts/insertion_test.go create mode 100644 sorts/merge.go create mode 100644 sorts/merge_test.go create mode 100644 sorts/selection.go create mode 100644 sorts/selection_test.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4fe9a6d..3891b05 100755 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/.golangci.yml b/.golangci.yml index 3e0ab98..54d9a69 100755 --- a/.golangci.yml +++ b/.golangci.yml @@ -371,5 +371,5 @@ linters: - exportloopref - gci - gosec - - lll +# - lll fast: false diff --git a/.gvmrc b/.gvmrc new file mode 100644 index 0000000..f51db4e --- /dev/null +++ b/.gvmrc @@ -0,0 +1 @@ +1.20.14 \ No newline at end of file diff --git a/LICENSE b/LICENSE index a34ec0f..88542dc 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ BSD 3-Clause License -Copyright (c) 2019-2023, Mikhail Knyazhev +Copyright (c) 2019-2024, Mikhail Knyazhev Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/README.md b/README.md index 818fc7b..9635ddf 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/go.mod b/go.mod index de55895..07683ac 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index fa4b6e6..60ce688 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/graph/kahn/type.go b/graph/kahn/type.go index 1c512f1..217bc1b 100644 --- a/graph/kahn/type.go +++ b/graph/kahn/type.go @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2023 Mikhail Knyazhev . All rights reserved. + * Copyright (c) 2019-2024 Mikhail Knyazhev . All rights reserved. * Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file. */ diff --git a/graph/kahn/type_test.go b/graph/kahn/type_test.go index 4912d87..54c0a92 100644 --- a/graph/kahn/type_test.go +++ b/graph/kahn/type_test.go @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2023 Mikhail Knyazhev . All rights reserved. + * Copyright (c) 2019-2024 Mikhail Knyazhev . All rights reserved. * Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file. */ diff --git a/shorten/shorten.go b/shorten/shorten.go index 8160133..fe8b4e4 100644 --- a/shorten/shorten.go +++ b/shorten/shorten.go @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2023 Mikhail Knyazhev . All rights reserved. + * Copyright (c) 2019-2024 Mikhail Knyazhev . All rights reserved. * Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file. */ diff --git a/shorten/shorten_test.go b/shorten/shorten_test.go index 6a39f9c..b18b882 100644 --- a/shorten/shorten_test.go +++ b/shorten/shorten_test.go @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2023 Mikhail Knyazhev . All rights reserved. + * Copyright (c) 2019-2024 Mikhail Knyazhev . All rights reserved. * Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file. */ diff --git a/sorts/bubble.go b/sorts/bubble.go new file mode 100644 index 0000000..c84aa43 --- /dev/null +++ b/sorts/bubble.go @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2019-2024 Mikhail Knyazhev . 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 + } + } +} diff --git a/sorts/bubble_test.go b/sorts/bubble_test.go new file mode 100644 index 0000000..04cc751 --- /dev/null +++ b/sorts/bubble_test.go @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2019-2024 Mikhail Knyazhev . 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] + }) + } +} diff --git a/sorts/cocktail.go b/sorts/cocktail.go new file mode 100644 index 0000000..fd05c36 --- /dev/null +++ b/sorts/cocktail.go @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019-2024 Mikhail Knyazhev . 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 + } + } +} diff --git a/sorts/cocktail_test.go b/sorts/cocktail_test.go new file mode 100644 index 0000000..5abac63 --- /dev/null +++ b/sorts/cocktail_test.go @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2019-2024 Mikhail Knyazhev . 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] + }) + } +} diff --git a/sorts/common_test.go b/sorts/common_test.go new file mode 100644 index 0000000..5035919 --- /dev/null +++ b/sorts/common_test.go @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2019-2024 Mikhail Knyazhev . 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] + }) + } +} diff --git a/sorts/insertion.go b/sorts/insertion.go new file mode 100644 index 0000000..52f1a35 --- /dev/null +++ b/sorts/insertion.go @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2019-2024 Mikhail Knyazhev . 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] + } + } +} diff --git a/sorts/insertion_test.go b/sorts/insertion_test.go new file mode 100644 index 0000000..6d00a26 --- /dev/null +++ b/sorts/insertion_test.go @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2019-2024 Mikhail Knyazhev . 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_SortInsertion(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.Insertion(tt.args, func(i, j int) bool { + return tt.args[i] < tt.args[j] + }) + require.Equal(t, tt.want, tt.args) + }) + } +} + +func Benchmark_SortInsertion(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.Insertion(arr, func(i, j int) bool { + return arr[i] < arr[j] + }) + } +} diff --git a/sorts/merge.go b/sorts/merge.go new file mode 100644 index 0000000..0d77cad --- /dev/null +++ b/sorts/merge.go @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2019-2024 Mikhail Knyazhev . 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/Merge_sort + +package sorts + +func Merge[T any](list []T, less func(i, j int) bool) { + tmp := make([]T, 0, len(list)/2+1) + mergeSplitSortJoin[T](list, tmp, 0, len(list)-1, less) +} + +func mergeSplitSortJoin[T any](list, tmp []T, from, to int, less func(i, j int) bool) { + size := to - from + 1 + switch true { + case size <= 1: + return + case size == 2: + if less(to, from) { + list[to], list[from] = list[from], list[to] + } + return + } + + half := size / 2 + + mergeSplitSortJoin[T](list, tmp, from+half, to, less) + mergeSplitSortJoin[T](list, tmp, from, from+half-1, less) + mergeJoin[T](list, tmp[:0], from, from+half-1, from+half, to, less) +} + +func mergeJoin[T any](list, tmp []T, fromA, toA, fromB, toB int, less func(i, j int) bool) { + nA, nB := fromA, fromB + for { + if nA <= toA && nB <= toB { + if less(nA, nB) { + tmp = append(tmp, list[nA]) + nA++ + } else { + tmp = append(tmp, list[nB]) + nB++ + } + continue + } + + if nB > toB { + for i := nA; i <= toA; i++ { + tmp = append(tmp, list[i]) + } + } else { + for i := nB; i <= toB; i++ { + tmp = append(tmp, list[i]) + } + } + break + } + for i, v := range tmp { + list[fromA+i] = v + } +} diff --git a/sorts/merge_test.go b/sorts/merge_test.go new file mode 100644 index 0000000..1d4d074 --- /dev/null +++ b/sorts/merge_test.go @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2019-2024 Mikhail Knyazhev . 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_SortMerge(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.Merge(tt.args, func(i, j int) bool { + return tt.args[i] < tt.args[j] + }) + require.Equal(t, tt.want, tt.args) + }) + } +} + +func Benchmark_SortMerge(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.Merge(arr, func(i, j int) bool { + return arr[i] < arr[j] + }) + } +} diff --git a/sorts/selection.go b/sorts/selection.go new file mode 100644 index 0000000..2d62ef5 --- /dev/null +++ b/sorts/selection.go @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2019-2024 Mikhail Knyazhev . 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/Selection_sort + +package sorts + +func Selection[T any](list []T, less func(i, j int) bool) { + if len(list) < 2 { + return + } + for i := 0; i < len(list)-1; i++ { + min := i + for j := i + 1; j < len(list); j++ { + if less(j, min) { + min = j + } + } + list[i], list[min] = list[min], list[i] + } +} diff --git a/sorts/selection_test.go b/sorts/selection_test.go new file mode 100644 index 0000000..83c144f --- /dev/null +++ b/sorts/selection_test.go @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2019-2024 Mikhail Knyazhev . 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_SortSelection(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.Selection(tt.args, func(i, j int) bool { + return tt.args[i] < tt.args[j] + }) + require.Equal(t, tt.want, tt.args) + }) + } +} + +func Benchmark_SortSelection(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.Selection(arr, func(i, j int) bool { + return arr[i] < arr[j] + }) + } +}