-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathcounting_sort_test.go
65 lines (53 loc) · 1.41 KB
/
counting_sort_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Problem:
- Implement counting sort.
Approach:
- Iterate through the input, count the number of times each item occurs, use
these counts to compute each item's index in the final sorted array.
Cost:
- O(n) time and O(n) space.
*/
package lab
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestCountingSort(t *testing.T) {
tests := []struct {
in []int
expected []int
}{
{[]int{}, []int{}},
{[]int{1}, []int{1}},
{[]int{1, 2}, []int{1, 2}},
{[]int{2, 1}, []int{1, 2}},
{[]int{2, 1, 3}, []int{1, 2, 3}},
{[]int{1, 1, 1}, []int{1, 1, 1}},
{[]int{2, 1, 2}, []int{1, 2, 2}},
{[]int{1, 2, 4, 3, 6, 5}, []int{1, 2, 3, 4, 5, 6}},
{[]int{6, 2, 4, 3, 1, 5}, []int{1, 2, 3, 4, 5, 6}},
{[]int{6, 5, 4, 3, 2, 1}, []int{1, 2, 3, 4, 5, 6}},
}
for _, tt := range tests {
result := countingSort(tt.in, 6)
common.Equal(t, tt.expected, result)
}
}
func countingSort(in []int, max int) []int {
// utilize max value to create a fix-sized list of item counts.
counts := make([]int, max+1)
out := make([]int, 0)
// populate the array where its indices represent items themselves and
// its values represent how many time the item appears.
for _, item := range in {
counts[item]++
}
// iterate through the counts and add the item to the output list.
for i := 0; i < len(counts); i++ {
count := counts[i]
for j := 0; j < count; j++ {
out = append(out, i)
}
}
return out
}