Skip to content

Commit ac6604b

Browse files
committed
Merge branch 'develop'
2 parents 083e34c + 35d3a6f commit ac6604b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+778
-288
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -920,9 +920,9 @@ Math.pow(2.71, 3.15), Math.round(3.14), Math.sqrt(225)
920920

921921
- Combination: [cpp](/cpp-algorithm/src/math/combination.h)(`GenerateCombination`) | Find the number of ways to choose $k$ items from $n$ items.
922922
- Fast Fourier transform: Fast Fourier transform is a mathematical algorithm that finds the discrete Fourier transform of a set of real numbers.
923-
- Greatest common divisor (GCD), CLRS#31.2: [python](/python-algorithm/algorithm/math/greatest_common_divisor.py), [java](/java-algorithm/src/main/java/com/example/algorithm/math/GreatestCommonDivisor.java), [golang](/go-algorithm/pkg/math/greatest_common_divisor.go) | Find the greatest common divisor of two numbers.
923+
- Greatest common divisor (GCD), CLRS#31.2: [python](/python-algorithm/algorithm/math/greatest_common_divisor.py), [golang](/go-algorithm/pkg/math/greatest_common_divisor.go), [java](/java-algorithm/src/main/java/com/example/algorithm/math/GreatestCommonDivisor.java) | Find the greatest common divisor of two numbers.
924924
- Integer factorization: [java](/java-algorithm/src/main/java/com/example/algorithm/math/IntegerFactorization.java) | Integer factorization is the process of determining which prime numbers divide a given positive integer.
925-
- Least common multiple (LCM): [python](/python-algorithm/algorithm/math/least_common_multiple.py), [java](/java-algorithm/src/main/java/com/example/algorithm/math/LeastCommonMultiple.java), [golang](/go-algorithm/pkg/math/least_common_multiple.go) | Find the least common multiple of two numbers.
925+
- Least common multiple (LCM): [python](/python-algorithm/algorithm/math/least_common_multiple.py), [golang](/go-algorithm/pkg/math/least_common_multiple.go), [java](/java-algorithm/src/main/java/com/example/algorithm/math/LeastCommonMultiple.java) | Find the least common multiple of two numbers.
926926
- Miller-Rabin primality test, CLRS#31.8: [cpp](/cpp-algorithm/src/math/miller_rabin.h) | Miller-Rabin primality test is a mathematical algorithm that finds whether a given number is prime.
927927
- Permutation
928928
- given string: [cpp](/cpp-algorithm/src/math/permutation.h)(`Permutation`) | Find the permutation of a set of items from given string.
@@ -1164,39 +1164,39 @@ list.sort(Comparator.comparingInt(String::length));
11641164

11651165
**Sorting algorithms**
11661166

1167-
- Bubble sort, CLRS#2-problems2.2: [java](/java-algorithm/src/main/java/com/example/algorithm/sort/BubbleSort.java) | Bubble sort is a sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items, and swaps them if needed.<br>($\textit{n}$ is the number of elements)
1167+
- Bubble sort, CLRS#2-problems2.2: [golang](go-algorithm/pkg/sort/bubble_sort.go), [java](/java-algorithm/src/main/java/com/example/algorithm/sort/BubbleSort.java) | Bubble sort is a sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items, and swaps them if needed.<br>($\textit{n}$ is the number of elements)
11681168

11691169
| **Case** | **Time complexity** | **Remarks** |
11701170
| :---------- | :-----------------: | :-------------------------------------------------------------------------------------- |
11711171
| **Best** | $O(n)$ | when the input list is already sorted in the desired order (ascending or descending) |
11721172
| **Worst** | $O(n^2)$ | when the input list is already sorted in the reverse order of the desired sorting order |
11731173
| **Average** | $O(n^2)$ | when the input list is in jumbled order |
11741174

1175-
- Bucket sort, CLRS#8.4: [java](/java-algorithm/src/main/java/com/example/algorithm/sort/BucketSort.java) | Bucket sort is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket contains a range of values and the elements are sorted within these buckets using any of the suitable sorting algorithms (such as insertion sort, merge sort, selection sort).<br>($\textit{n}$ is the number of elements and $\textit{k}$ is the number of buckets)
1175+
- Bucket sort, CLRS#8.4: [golang](go-algorithm/pkg/sort/bucket_sort.go), [java](/java-algorithm/src/main/java/com/example/algorithm/sort/BucketSort.java) | Bucket sort is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket contains a range of values and the elements are sorted within these buckets using any of the suitable sorting algorithms (such as insertion sort, merge sort, selection sort).<br>($\textit{n}$ is the number of elements and $\textit{k}$ is the number of buckets)
11761176

11771177
| **Case** | **Time complexity** | **Remarks** |
11781178
| ----------- | :-----------------: | -------------------------------------------------------------------------------------------------------------- |
11791179
| **Best** | $O(n + k)$ | when input elements are uniformly distributed<br> and each bucket contains roughly the same number of elements |
11801180
| **Worst** | $O(n^2)$ | when all elements are placed into a single bucket |
11811181
| **Average** | $O(n)$ | |
11821182

1183-
- Counting sort, CLRS#8.2: [java](/java-algorithm/src/main/java/com/example/algorithm/sort/CountingSort.java) | Counting sort is a non-comparative sorting algorithm that sorts the elements of an array by counting the occurrences of each element in the array. The count is stored in an auxiliary array and the sorting is done by mapping the count as an index of the auxiliary array. It is used as a subroutine in radix sort (CLRS#8.3).<br>($\textit{n}$ is the number of elements and $\textit{k}$ is the range of input values)
1183+
- Counting sort, CLRS#8.2: [golang](go-algorithm/pkg/sort/counting_sort.go), [java](/java-algorithm/src/main/java/com/example/algorithm/sort/CountingSort.java) | Counting sort is a non-comparative sorting algorithm that sorts the elements of an array by counting the occurrences of each element in the array. The count is stored in an auxiliary array and the sorting is done by mapping the count as an index of the auxiliary array. It is used as a subroutine in radix sort (CLRS#8.3).<br>($\textit{n}$ is the number of elements and $\textit{k}$ is the range of input values)
11841184

11851185
| **Case** | **Time complexity** | **Remarks** |
11861186
| ----------- | :-----------------: | ------------------------------------------------------- |
11871187
| **Best** | $O(n + k)$ | when the input elements have a small range of values |
11881188
| **Worst** | $O(n + k)$ | when the input elements have a large range of values |
11891189
| **Average** | $O(n + k)$ | when the elements are distributed randomly in the array |
11901190

1191-
- Heap sort, CLRS#6: [java](/java-algorithm/src/main/java/com/example/algorithm/sort/HeapSort.java) | Heap sort is a comparison-based sorting algorithm that uses a binary heap data structure to sort an array. It is used for the implementation of priority queue.<br>($\textit{n}$ is the number of elements)
1191+
- Heap sort, CLRS#6: [golang](go-algorithm/pkg/sort/heap_sort.go), [java](/java-algorithm/src/main/java/com/example/algorithm/sort/HeapSort.java) | Heap sort is a comparison-based sorting algorithm that uses a binary heap data structure to sort an array. It is used for the implementation of priority queue.<br>($\textit{n}$ is the number of elements)
11921192

11931193
| **Case** | **Time complexity** | **Remarks** |
11941194
| ----------- | :-----------------: | ----------- |
11951195
| **Best** | $O(n log n)$ | |
11961196
| **Worst** | $O(n log n)$ | |
11971197
| **Average** | $O(n log n)$ | |
11981198

1199-
- Insertion sort, CLRS#2.1: [cpp](/cpp-algorithm/src/sort/sort.h), [java](/java-algorithm/src/main/java/com/example/algorithm/sort/InsertionSort.java) | Insertion sort is a comparison-based sorting algorithm that builds the final sorted array one element at a time. One of the fastest algorithms for sorting very small arrays (around 10 elements).<br>($\textit{n}$ is the number of elements)
1199+
- Insertion sort, CLRS#2.1: [cpp](/cpp-algorithm/src/sort/sort.h), [golang](go-algorithm/pkg/sort/insertion_sort.go), [java](/java-algorithm/src/main/java/com/example/algorithm/sort/InsertionSort.java) | Insertion sort is a comparison-based sorting algorithm that builds the final sorted array one element at a time. One of the fastest algorithms for sorting very small arrays (around 10 elements).<br>($\textit{n}$ is the number of elements)
12001200

12011201
| **Case** | **Time complexity** | **Remarks** |
12021202
| ----------- | :-----------------: | ------------------------------------------------------------------------------- |

go-algorithm/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ require github.com/stretchr/testify v1.9.0
77
require (
88
github.com/davecgh/go-spew v1.1.1 // indirect
99
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
1011
gopkg.in/yaml.v3 v3.0.1 // indirect
1112
)

go-algorithm/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
44
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
55
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
66
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7+
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
8+
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
79
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
810
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
911
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Lines changed: 88 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,97 @@
11
package math
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
5+
6+
"github.com/stretchr/testify/assert"
67
)
78

8-
func TestGCDEuclidean(t *testing.T) {
9-
assert.Equal(t, 12, GCDEuclidean(24, 36))
10-
assert.Equal(t, 1, GCDEuclidean(17, 22))
11-
assert.Equal(t, 20, GCDEuclidean(120, 500))
9+
func Test_GCDEuclidean(t *testing.T) {
10+
type args struct {
11+
a int
12+
b int
13+
}
14+
tests := []struct {
15+
name string
16+
args args
17+
want int
18+
}{
19+
{
20+
name: "Case 1",
21+
args: args{
22+
a: 24,
23+
b: 36,
24+
},
25+
want: 12,
26+
},
27+
{
28+
name: "Case 2",
29+
args: args{
30+
a: 17,
31+
b: 22,
32+
},
33+
want: 1,
34+
},
35+
{
36+
name: "Case 3",
37+
args: args{
38+
a: 120,
39+
b: 500,
40+
},
41+
want: 20,
42+
},
43+
}
44+
45+
for _, tt := range tests {
46+
t.Run(tt.name, func(t *testing.T) {
47+
got := GCDEuclidean(tt.args.a, tt.args.b)
48+
49+
assert.Equalf(t, tt.want, got, "GCDEuclidean(%v, %v)", tt.args.a, tt.args.b)
50+
})
51+
}
1252
}
1353

14-
func TestGCDEuclideanExtended(t *testing.T) {
15-
d1, _, _ := GCDEuclideanExtended(24, 36)
16-
assert.Equal(t, 12, d1)
17-
d2, _, _ := GCDEuclideanExtended(17, 22)
18-
assert.Equal(t, 1, d2)
19-
d3, _, _ := GCDEuclideanExtended(120, 500)
20-
assert.Equal(t, 20, d3)
54+
func Test_GCDEuclideanExtended(t *testing.T) {
55+
type args struct {
56+
a int
57+
b int
58+
}
59+
tests := []struct {
60+
name string
61+
args args
62+
want int
63+
}{
64+
{
65+
name: "Case 1",
66+
args: args{
67+
a: 24,
68+
b: 36,
69+
},
70+
want: 12,
71+
},
72+
{
73+
name: "Case 2",
74+
args: args{
75+
a: 17,
76+
b: 22,
77+
},
78+
want: 1,
79+
},
80+
{
81+
name: "Case 3",
82+
args: args{
83+
a: 120,
84+
b: 500,
85+
},
86+
want: 20,
87+
},
88+
}
89+
90+
for _, tt := range tests {
91+
t.Run(tt.name, func(t *testing.T) {
92+
got, _, _ := GCDEuclideanExtended(tt.args.a, tt.args.b)
93+
94+
assert.Equalf(t, tt.want, got, "GCDEuclideanExtended(%v, %v)", tt.args.a, tt.args.b)
95+
})
96+
}
2197
}
Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,52 @@
11
package math
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
5+
6+
"github.com/stretchr/testify/assert"
67
)
78

8-
func TestLCM(t *testing.T) {
9-
assert.Equal(t, 72, LCM(24, 36))
10-
assert.Equal(t, 374, LCM(17, 22))
11-
assert.Equal(t, 3000, LCM(120, 500))
9+
func Test_LCM(t *testing.T) {
10+
type args struct {
11+
a int
12+
b int
13+
}
14+
tests := []struct {
15+
name string
16+
args args
17+
want int
18+
}{
19+
{
20+
name: "Case 1",
21+
args: args{
22+
a: 24,
23+
b: 36,
24+
},
25+
want: 72,
26+
},
27+
{
28+
name: "Case 2",
29+
args: args{
30+
a: 17,
31+
b: 22,
32+
},
33+
want: 374,
34+
},
35+
{
36+
name: "Case 3",
37+
args: args{
38+
a: 120,
39+
b: 500,
40+
},
41+
want: 3000,
42+
},
43+
}
44+
45+
for _, tt := range tests {
46+
t.Run(tt.name, func(t *testing.T) {
47+
got := LCM(tt.args.a, tt.args.b)
48+
49+
assert.Equalf(t, tt.want, got, "LCM() got = %v, want %v", got, tt.want)
50+
})
51+
}
1252
}

go-algorithm/pkg/sort/bubble_sort.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package sort
2+
3+
import (
4+
"golang.org/x/exp/constraints"
5+
)
6+
7+
func bubbleSort[T constraints.Integer](arr []T) {
8+
n := len(arr)
9+
for i := 0; i < n; i++ {
10+
for j := n - 1; j > i; j-- {
11+
if arr[j] < arr[j-1] {
12+
arr[j], arr[j-1] = arr[j-1], arr[j]
13+
}
14+
}
15+
}
16+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package sort
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_bubbleSort(t *testing.T) {
10+
type args struct {
11+
arr []int
12+
}
13+
tests := []struct {
14+
name string
15+
args args
16+
want []int
17+
}{
18+
{
19+
name: "Integer Case",
20+
args: args{
21+
arr: []int{31, 64, 49, 85, 71, 26, 6, 19},
22+
},
23+
want: []int{6, 19, 26, 31, 49, 64, 71, 85},
24+
},
25+
{
26+
name: "Empty array input",
27+
args: args{
28+
arr: []int{},
29+
},
30+
want: []int{},
31+
},
32+
}
33+
34+
for _, tt := range tests {
35+
t.Run(tt.name, func(t *testing.T) {
36+
bubbleSort(tt.args.arr)
37+
38+
assert.Equalf(t, tt.want, tt.args.arr, "bubbleSort() got = %v, want %v", tt.args.arr, tt.want)
39+
})
40+
}
41+
}

go-algorithm/pkg/sort/bucket_sort.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package sort
2+
3+
import (
4+
"math"
5+
6+
"golang.org/x/exp/constraints"
7+
)
8+
9+
func bucketSort[T constraints.Integer](arr []T) []T {
10+
n := len(arr)
11+
12+
var maxVal, minVal T
13+
for _, v := range arr {
14+
if v > maxVal {
15+
maxVal = v
16+
}
17+
if v < minVal {
18+
minVal = v
19+
}
20+
}
21+
rangeVal := maxVal - minVal
22+
23+
bucket := make([][]T, n)
24+
for i := 0; i < n; i++ {
25+
bucket[i] = make([]T, 0)
26+
}
27+
28+
for _, v := range arr {
29+
index := int(math.Floor(float64(n) * float64((v-minVal)/rangeVal)))
30+
if index == n {
31+
index--
32+
}
33+
bucket[index] = append(bucket[index], v)
34+
}
35+
36+
for i := 0; i < n; i++ {
37+
insertionSort(bucket[i])
38+
}
39+
40+
sorted := make([]T, 0, n)
41+
for i := 0; i < n; i++ {
42+
sorted = append(sorted, bucket[i]...)
43+
}
44+
45+
return sorted
46+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package sort
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_bucketSortInteger(t *testing.T) {
10+
type args struct {
11+
arr []int
12+
}
13+
tests := []struct {
14+
name string
15+
args args
16+
want []int
17+
}{
18+
{
19+
name: "Integer Case",
20+
args: args{
21+
arr: []int{31, 64, 49, 85, 71, 26, 6, 19},
22+
},
23+
want: []int{6, 19, 26, 31, 49, 64, 71, 85},
24+
},
25+
{
26+
name: "Empty array input",
27+
args: args{
28+
arr: []int{},
29+
},
30+
want: []int{},
31+
},
32+
}
33+
34+
for _, tt := range tests {
35+
t.Run(tt.name, func(t *testing.T) {
36+
got := bucketSort(tt.args.arr)
37+
38+
assert.Equalf(t, tt.want, got, "bucketSort() got = %v, want %v", got, tt.want)
39+
})
40+
}
41+
}

0 commit comments

Comments
 (0)