-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounting_sort.go
66 lines (58 loc) · 1.26 KB
/
counting_sort.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
66
package main
import (
"flag"
"fmt"
"strconv"
)
const intMin = ^int(^uint(0) >> 1)
func countingSort(source []int, result []int, max int, show, desc bool) {
temp := make([]int, max+1)
for _, value := range source {
temp[value]++
}
if desc {
for i := max - 1; i >= 0; i-- {
temp[i] += temp[i+1]
}
} else {
for i := 1; i <= max; i++ {
temp[i] += temp[i-1]
}
}
if show {
fmt.Println("sort: temp counting array init success", temp)
}
for j := len(source) - 1; j >= 0; j-- {
if show {
fmt.Println("sort: result index", temp[source[j]], "get value", source[j])
}
result[temp[source[j]]-1] = source[j]
temp[source[j]]--
if show {
fmt.Println("sort: temp counting array state", temp)
fmt.Println("sort: result state", result)
}
}
}
func max(source []int) int {
maxValue := intMin
for _, value := range source {
if value > maxValue {
maxValue = value
}
}
return maxValue
}
func main() {
show := flag.Bool("s", false, "show detail flag")
desc := flag.Bool("d", false, "desc flag")
flag.Parse()
source := []int{}
for _, arg := range flag.Args() {
i, _ := strconv.Atoi(arg)
source = append(source, i)
}
result := make([]int, len(source))
countingSort(source, result, max(source), *show, *desc)
fmt.Println(result)
}