Skip to content

Commit 6f4c635

Browse files
author
mattan
committed
Initial Commit
1 parent 6629aad commit 6f4c635

File tree

2 files changed

+101
-7
lines changed

2 files changed

+101
-7
lines changed

sorts/radix_sort.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package sorts
2+
3+
import (
4+
"math"
5+
)
6+
7+
func max(arr []int) int {
8+
max := math.MinInt64
9+
for _, item := range arr {
10+
if item > max {
11+
max = item
12+
}
13+
}
14+
return max
15+
}
16+
17+
func countSort(arr []int, exp int) []int {
18+
var digits [10]int
19+
var output = make([]int, len(arr))
20+
for _, item := range arr {
21+
digits[(item/exp)%10]++
22+
}
23+
24+
for i := 1; i < 10; i++ {
25+
digits[i] += digits[i-1]
26+
}
27+
28+
for i := len(arr) - 1; i >= 0; i-- {
29+
output[digits[(arr[i]/exp)%10]-1] = arr[i]
30+
digits[(arr[i]/exp)%10]--
31+
}
32+
33+
return output
34+
}
35+
36+
func RadixSort(arr []int) []int {
37+
maxElement := max(arr)
38+
for exp := 1; maxElement/exp > 0; exp *= 10 {
39+
arr = countSort(arr, exp)
40+
}
41+
return arr
42+
}
43+
44+
// The following adds support for signed ints
45+
/*
46+
47+
func unsignedRadixSort(arr []int) []int {
48+
maxElement := max(arr)
49+
for exp := 1; maxElement/exp > 0; exp *= 10 {
50+
arr = countSort(arr, exp)
51+
}
52+
return arr
53+
}
54+
55+
func RadixSort(arr []int) []int {
56+
var negatives, nonNegatives []int
57+
58+
for _, item := range arr {
59+
if item < 0 {
60+
negatives = append(negatives, -item)
61+
} else {
62+
nonNegatives = append(nonNegatives, item)
63+
}
64+
}
65+
negatives = unsignedRadixSort(negatives)
66+
67+
// Reverse the negative array and restore signs
68+
for i, j := 0, len(negatives) -1; i < j; i,j = i + 1, j - 1 {
69+
negatives[i], negatives[j] = -negatives[j], -negatives[i]
70+
}
71+
return append(negatives, unsignedRadixSort(nonNegatives)...)
72+
}
73+
*/

sorts/sorts_test.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ func TestShell(t *testing.T) {
9595
}
9696
}
9797

98+
func TestRadix(t *testing.T) {
99+
for _, test := range sortTests {
100+
actual := RadixSort(test.input)
101+
pos, sorted := compareSlices(actual, test.expected)
102+
if !sorted {
103+
if pos == -1 {
104+
t.Errorf("test %s failed due to slice length changing", test.name)
105+
}
106+
t.Errorf("test %s failed at index %d", test.name, pos)
107+
}
108+
}
109+
}
110+
98111
/*func TestTopological(t *testing.T) {
99112
for _, test := range sortTests {
100113
actual := topologicalSort(test.input)
@@ -119,13 +132,13 @@ func BenchmarkBubble(b *testing.B) {
119132
}
120133
}
121134

122-
func BenchmarkSelection(b *testing.B) {
123-
for i := 0; i < b.N; i++ {
124-
for _, test := range sortTests {
125-
selectionSort(test.input)
126-
}
127-
}
128-
}
135+
//func BenchmarkSelection(b *testing.B) {
136+
// for i := 0; i < b.N; i++ {
137+
// for _, test := range sortTests {
138+
// selectionSort(test.input)
139+
// }
140+
// }
141+
//}
129142

130143
func BenchmarkInsertion(b *testing.B) {
131144
for i := 0; i < b.N; i++ {
@@ -167,6 +180,14 @@ func BenchmarkShell(b *testing.B) {
167180
}
168181
}
169182

183+
func BenchmarkRadix(b *testing.B) {
184+
for i := 0; i < b.N; i++ {
185+
for _, test := range sortTests {
186+
RadixSort(test.input)
187+
}
188+
}
189+
}
190+
170191
/*func BenchmarkTopological(b *testing.B) {
171192
for i := 0; i < b.N; i++ {
172193
for _, test := range sortTests {

0 commit comments

Comments
 (0)