forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counting_sort.go
45 lines (34 loc) · 806 Bytes
/
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
package main
import "fmt"
func CountingSort(slice []int) {
if len(slice) < 1 {
return
}
higher := slice[0]
for index := 1; index < len(slice); index++ {
if slice[index] > higher {
higher = slice[index]
}
}
var temp = make([]int, higher)
for index := 0; index < len(slice); index++ {
temp[slice[index]-1]++
}
for index := 1; index < len(temp); index++ {
temp[index] += temp[index-1]
}
var temp2 = make([]int, len(slice))
for index := 0; index < len(temp2); index++ {
temp2[temp[slice[index]-1]-1] = slice[index]
temp[slice[index]-1]--
}
for index := 0; index < len(temp2); index++ {
slice[index] = temp2[index]
}
}
func main() {
slice := []int{9, 5, 8, 7, 3, 2, 1, 6, 4}
fmt.Println("Slice:", slice)
CountingSort(slice)
fmt.Println("CountingSort:", slice)
}