summaryrefslogtreecommitdiff
path: root/scratchdir/radixsort.go
blob: 806a5f8baaa55b72f8a029be92bbe2d2a984be2c (plain)
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
package main

import (
	"fmt"
	"math/rand"
)

const (
	signedMin = -(1 << 31)
	signedMax = (1 << 31) - 1
)

func countsort(in []int32, r uint32) {
	count := make([]int32, r)

	for i := range in {
		count[in[i]]++
	}

	for i := 1; i < len(in)-1; i++ {
		count[i] += count[i-1]
	}

	var pointer int32
	for i := 0; i < len(count)-1; i++ {
		for count[i] > 0 && pointer < count[i] {
			in[pointer] = int32(i)
			pointer++
		}
	}
}

func radix() {
}

func input(r *rand.Rand, n int) []int32 {
	out := make([]int32, n)
	for i := 0; i < n; i++ {
		out[i] = int32(r.Intn(signedMax))
	}
	return out
}

func main() {
	in := input(rand.New(rand.NewSource(1)), 1<<8)
	countsort(in, signedMax)
	fmt.Println(in)
}