Skip to content

Commit 9aac648

Browse files
committed
feat: add counting sort
1 parent de46d60 commit 9aac648

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ list.sort(Comparator.comparingInt(String::length));
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
| ----------- | :-----------------: | ------------------------------------------------------- |
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package sort
2+
3+
import (
4+
"golang.org/x/exp/constraints"
5+
)
6+
7+
func countingSort[T constraints.Integer](arr []T) []T {
8+
n := len(arr)
9+
output := make([]T, n)
10+
11+
var maxVal T
12+
for _, v := range arr {
13+
if v > maxVal {
14+
maxVal = v
15+
}
16+
}
17+
18+
// initialize count array
19+
count := make([]int, maxVal+1)
20+
21+
// store the count of each element
22+
for i := 0; i < n; i++ {
23+
count[arr[i]]++
24+
}
25+
26+
// store the cumulative count of each element
27+
for i := 1; i <= int(maxVal); i++ {
28+
count[i] += count[i-1]
29+
}
30+
31+
// build the output array
32+
for i := n - 1; i >= 0; i-- {
33+
output[count[arr[i]]-1] = arr[i]
34+
count[arr[i]]--
35+
}
36+
37+
return output
38+
}
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+
"golang.org/x/exp/constraints"
8+
)
9+
10+
func Test_countingSort(t *testing.T) {
11+
type args[T constraints.Integer] struct {
12+
arr []T
13+
}
14+
type testCase[T constraints.Integer] struct {
15+
name string
16+
args args[T]
17+
want []T
18+
}
19+
tests := []testCase[int]{
20+
{
21+
name: "Integer Case",
22+
args: args[int]{
23+
arr: []int{31, 64, 49, 85, 71, 26, 6, 19},
24+
},
25+
want: []int{6, 19, 26, 31, 49, 64, 71, 85},
26+
},
27+
{
28+
name: "Empty array input",
29+
args: args[int]{},
30+
want: []int{},
31+
},
32+
}
33+
34+
for _, tt := range tests {
35+
t.Run(tt.name, func(t *testing.T) {
36+
got := countingSort(tt.args.arr)
37+
38+
assert.Equalf(t, tt.want, got, "countingSort() got = %v, want %v", got, tt.want)
39+
})
40+
}
41+
}

0 commit comments

Comments
 (0)