-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
533 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.20.14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_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] | ||
}) | ||
} | ||
} |
Oops, something went wrong.