forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountingSort.go
54 lines (45 loc) · 819 Bytes
/
CountingSort.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
package main
import (
"fmt"
"log"
"time"
)
func CountingSort(nums []int, min, max int) {
n := len(nums)
k := max - min + 1
c := make([]int, k)
for _, v := range nums {
c[v-min]++
}
log.Println(c)
for i := 1; i < k; i++ {
c[i] += c[i-1]
}
log.Println(c)
r := make([]int, n)
for i := n - 1; i >= 0; i-- {
v := nums[i]
a := c[v-min]
r[a-1] = v
c[v-min]--
log.Println(r)
}
for i, v := range r {
nums[i] = v
}
log.Println(nums)
}
func main() {
// uncomment following line to enable log print
// log.SetOutput(io.Discard)
// test case 1
nums := []int{1, 2, 7, 9, 5, 5, 8}
CountingSort(nums, 1, 9)
fmt.Println(nums)
// wait complete output to terminal
time.Sleep(time.Second)
// test case 2
nums = []int{3, 7, 9, 5, 5, 8, 11}
CountingSort(nums, 3, 11)
fmt.Println(nums)
}