Skip to content

Commit 543fa8d

Browse files
committed
Refactor counting sort and add test
1 parent 93c51b4 commit 543fa8d

File tree

4 files changed

+40
-46
lines changed

4 files changed

+40
-46
lines changed

sorts/bubble_sort.go renamed to sorts/bubblesort.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Package sorts a package for demonstrating sorting algorithms in Go
21
// Implementation of basic bubble sort algorithm
32
// Reference: https://en.wikipedia.org/wiki/Bubble_sort
43

sorts/counting_sort.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

sorts/countingsort.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Reference: https://en.wikipedia.org/wiki/Counting_sort
2+
3+
package sorts
4+
5+
func countingSort(arr []int) []int {
6+
if len(arr) == 0 {
7+
return make([]int, 0)
8+
}
9+
maxElement := arr[0]
10+
for i := 1; i < len(arr); i++ {
11+
if arr[i] > maxElement {
12+
maxElement = arr[i]
13+
}
14+
}
15+
16+
counts := make([]int, maxElement+1)
17+
result := make([]int, 0)
18+
for _, item := range arr {
19+
counts[item]++
20+
}
21+
for i := 0; i <= maxElement; i++ {
22+
for j := 0; j < counts[i]; j++ {
23+
result = append(result, i)
24+
}
25+
}
26+
return result
27+
}

sorts/sorts_test.go

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package sorts
22

33
import "testing"
44

5-
//BEGIN TESTS
6-
75
func TestBubble(t *testing.T) {
86
for _, test := range sortTests {
97
actual := bubbleSort(test.input)
@@ -17,6 +15,19 @@ func TestBubble(t *testing.T) {
1715
}
1816
}
1917

18+
func TestCountingSort(t *testing.T) {
19+
for _, testcase := range sortTests {
20+
actual := countingSort(testcase.input)
21+
pos, sorted := compareSlices(actual, testcase.expected)
22+
if !sorted {
23+
if pos == -1 {
24+
t.Errorf("test %s failed due to slice length changing", testcase.name)
25+
}
26+
t.Errorf("test %s failed at index %d", testcase.name, pos)
27+
}
28+
}
29+
}
30+
2031
func TestSelection(t *testing.T) {
2132
for _, test := range sortTests {
2233
actual := selectionSort(test.input)
@@ -95,22 +106,6 @@ func TestShell(t *testing.T) {
95106
}
96107
}
97108

98-
/*func TestTopological(t *testing.T) {
99-
for _, test := range sortTests {
100-
actual := topologicalSort(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-
111-
//END TESTS
112-
113-
//BEGIN BENCHMARKS
114109
func BenchmarkBubble(b *testing.B) {
115110
for i := 0; i < b.N; i++ {
116111
for _, test := range sortTests {
@@ -167,16 +162,6 @@ func BenchmarkShell(b *testing.B) {
167162
}
168163
}
169164

170-
/*func BenchmarkTopological(b *testing.B) {
171-
for i := 0; i < b.N; i++ {
172-
for _, test := range sortTests {
173-
topologicalSort(test.input)
174-
}
175-
}
176-
}*/
177-
178-
//END BENCHMARKS
179-
180165
func compareSlices(a []int, b []int) (int, bool) {
181166
if len(a) != len(b) {
182167
return -1, false

0 commit comments

Comments
 (0)