Skip to content

Commit 96c609f

Browse files
c4llmeco4chcclauss
authored andcommitted
Pull request regarding benchmarks and tests (#102)
* Updated packages, removed main from all but topological sort * Commented out main for the time being * Removed main from remaining files * Fixed naming issue * Renamed heap_sort, top_sort, deleted copy of select, insert * Added sorts_test and test cases * Updated linter to check sorts * Updated selection sort to return []int for testing * Changed large test to 500k for timeout purposes * Bubblesort returns a slice for testing purposes * Added return type to shell sort * Added tests and benchmarks for each sort * Commented out top sort temporarily * Removed prints from heap * Removed ineffectual assignments in selection sort * Commenting out top sort as a bandaid fix
1 parent 87c263d commit 96c609f

File tree

7 files changed

+186
-38
lines changed

7 files changed

+186
-38
lines changed

sorts/bubble_sort.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
//Package sorts a package for demonstrating sorting algorithms in Go
2-
package sorts
3-
4-
func bubbleSort(arrayzor []int) {
5-
6-
swapped := true
7-
for swapped {
8-
swapped = false
9-
for i := 0; i < len(arrayzor)-1; i++ {
10-
if arrayzor[i+1] < arrayzor[i] {
11-
swap(arrayzor, i, i+1)
12-
swapped = true
13-
}
14-
}
15-
}
16-
}
17-
18-
func swap(arrayzor []int, i, j int) {
19-
tmp := arrayzor[j]
20-
arrayzor[j] = arrayzor[i]
21-
arrayzor[i] = tmp
22-
}
1+
//Package sorts a package for demonstrating sorting algorithms in Go
2+
package sorts
3+
4+
func bubbleSort(arrayzor []int) []int {
5+
6+
7+
swapped := true
8+
for swapped {
9+
swapped = false
10+
for i := 0; i < len(arrayzor)-1; i++ {
11+
if arrayzor[i+1] < arrayzor[i] {
12+
swap(arrayzor, i, i+1)
13+
swapped = true
14+
}
15+
}
16+
}
17+
return arrayzor
18+
}
19+
20+
func swap(arrayzor []int, i, j int) {
21+
tmp := arrayzor[j]
22+
arrayzor[j] = arrayzor[i]
23+
arrayzor[i] = tmp
24+
}

sorts/heap_sort.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
//Package sorts a package for demonstrating sorting algorithms in Go
22
package sorts
33

4-
import (
5-
"fmt"
6-
)
7-
84
type MaxHeap struct {
95
slice []int
106
heapSize int
@@ -44,12 +40,12 @@ func heapSort(slice []int) []int {
4440
h.slice[0], h.slice[i] = h.slice[i], h.slice[0]
4541
h.heapSize--
4642
h.MaxHeapify(0)
47-
if i == len(h.slice)-1 || i == len(h.slice)-3 || i == len(h.slice)-5 {
43+
/*if i == len(h.slice)-1 || i == len(h.slice)-3 || i == len(h.slice)-5 {
4844
element := (i - len(h.slice)) * -1
4945
fmt.Println("Heap after removing ", element, " elements")
5046
fmt.Println(h.slice)
5147
52-
}
48+
}*/
5349
}
5450
return h.slice
5551
}

sorts/selection_sort.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
//Package sorts a package for demonstrating sorting algorithms in Go
22
package sorts
33

4-
func selectionSort(arr []int) {
5-
var min int = 0
6-
var tmp int = 0
4+
func selectionSort(arr []int) []int {
75

86
for i := 0; i < len(arr); i++ {
9-
min = i
7+
min := i
108
for j := i + 1; j < len(arr); j++ {
119
if arr[j] < arr[min] {
1210
min = j
1311
}
1412
}
1513

16-
tmp = arr[i]
14+
tmp := arr[i]
1715
arr[i] = arr[min]
1816
arr[min] = tmp
1917
}
18+
return arr
2019
}

sorts/shell_sort.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//Package sorts a package for demonstrating sorting algorithms in Go
22
package sorts
33

4-
func shellSort(arr []int) {
4+
func shellSort(arr []int) []int {
55
for d := int(len(arr) / 2); d > 0; d /= 2 {
66
for i := d; i < len(arr); i++ {
77
for j := i; j >= d && arr[j-d] > arr[j]; j -= d {
88
arr[j], arr[j-d] = arr[j-d], arr[j]
99
}
1010
}
1111
}
12+
return arr
1213
}

sorts/sorts_case_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ type sortTest struct {
1313
name string
1414
}
1515

16-
var arr []int = makeRandArray(1_000_000)
16+
var arr []int = makeRandArray(500_000)
17+
1718

1819
var sortTests = []sortTest{
1920
//Sorted slice
@@ -26,7 +27,8 @@ var sortTests = []sortTest{
2627
{[]int{}, []int{}, "Empty"},
2728
//Single-entry slice
2829
{[]int{1}, []int{1}, "Singleton"},
29-
//1M values sort
30+
31+
//500k values sort
3032
{arr, getSortedVersion(arr), "Large Random"},
3133
}
3234

sorts/sorts_test.go

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,46 @@ package sorts
33
import "testing"
44

55
//BEGIN TESTS
6+
7+
func TestBubble(t *testing.T) {
8+
for _, test := range sortTests {
9+
actual := bubbleSort(test.input)
10+
pos, sorted := compareSlices(actual, test.expected)
11+
if !sorted {
12+
if pos == -1 {
13+
t.Errorf("test %s failed due to slice length changing", test.name)
14+
}
15+
t.Errorf("test %s failed at index %d", test.name, pos)
16+
}
17+
}
18+
}
19+
20+
func TestSelection(t *testing.T) {
21+
for _, test := range sortTests {
22+
actual := selectionSort(test.input)
23+
pos, sorted := compareSlices(actual, test.expected)
24+
if !sorted {
25+
if pos == -1 {
26+
t.Errorf("test %s failed due to slice length changing", test.name)
27+
}
28+
t.Errorf("test %s failed at index %d", test.name, pos)
29+
}
30+
}
31+
}
32+
33+
func TestInsertion(t *testing.T) {
34+
for _, test := range sortTests {
35+
actual := insertionSort(test.input)
36+
pos, sorted := compareSlices(actual, test.expected)
37+
if !sorted {
38+
if pos == -1 {
39+
t.Errorf("test %s failed due to slice length changing", test.name)
40+
}
41+
t.Errorf("test %s failed at index %d", test.name, pos)
42+
}
43+
}
44+
}
45+
646
func TestMerge(t *testing.T) {
747
for _, test := range sortTests {
848
actual := Mergesort(test.input)
@@ -16,9 +56,85 @@ func TestMerge(t *testing.T) {
1656
}
1757
}
1858

59+
func TestHeap(t *testing.T) {
60+
for _, test := range sortTests {
61+
actual := heapSort(test.input)
62+
pos, sorted := compareSlices(actual, test.expected)
63+
if !sorted {
64+
if pos == -1 {
65+
t.Errorf("test %s failed due to slice length changing", test.name)
66+
}
67+
t.Errorf("test %s failed at index %d", test.name, pos)
68+
}
69+
}
70+
}
71+
72+
func TestQuick(t *testing.T) {
73+
for _, test := range sortTests {
74+
actual := quickSort(test.input)
75+
pos, sorted := compareSlices(actual, test.expected)
76+
if !sorted {
77+
if pos == -1 {
78+
t.Errorf("test %s failed due to slice length changing", test.name)
79+
}
80+
t.Errorf("test %s failed at index %d", test.name, pos)
81+
}
82+
}
83+
}
84+
85+
func TestShell(t *testing.T) {
86+
for _, test := range sortTests {
87+
actual := shellSort(test.input)
88+
pos, sorted := compareSlices(actual, test.expected)
89+
if !sorted {
90+
if pos == -1 {
91+
t.Errorf("test %s failed due to slice length changing", test.name)
92+
}
93+
t.Errorf("test %s failed at index %d", test.name, pos)
94+
}
95+
}
96+
}
97+
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+
19111
//END TESTS
20112

21113
//BEGIN BENCHMARKS
114+
func BenchmarkBubble(b *testing.B) {
115+
for i := 0; i < b.N; i++ {
116+
for _, test := range sortTests {
117+
bubbleSort(test.input)
118+
}
119+
}
120+
}
121+
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+
}
129+
130+
func BenchmarkInsertion(b *testing.B) {
131+
for i := 0; i < b.N; i++ {
132+
for _, test := range sortTests {
133+
insertionSort(test.input)
134+
}
135+
}
136+
}
137+
22138
func BenchmarkMerge(b *testing.B) {
23139
for i := 0; i < b.N; i++ {
24140
for _, test := range sortTests {
@@ -27,6 +143,38 @@ func BenchmarkMerge(b *testing.B) {
27143
}
28144
}
29145

146+
func BenchmarkHeap(b *testing.B) {
147+
for i := 0; i < b.N; i++ {
148+
for _, test := range sortTests {
149+
heapSort(test.input)
150+
}
151+
}
152+
}
153+
154+
func BenchmarkQuick(b *testing.B) {
155+
for i := 0; i < b.N; i++ {
156+
for _, test := range sortTests {
157+
quickSort(test.input)
158+
}
159+
}
160+
}
161+
162+
func BenchmarkShell(b *testing.B) {
163+
for i := 0; i < b.N; i++ {
164+
for _, test := range sortTests {
165+
shellSort(test.input)
166+
}
167+
}
168+
}
169+
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+
30178
//END BENCHMARKS
31179

32180
func compareSlices(a []int, b []int) (int, bool) {

sorts/topological_sort.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//Package sorts a package for demonstrating sorting algorithms in Go
22
package sorts
33

4-
type Dependency struct {
4+
/*type Dependency struct {
55
nodeId int
66
children []int
77
}
@@ -81,7 +81,7 @@ func topologicalSort(input []Edge) ([]int, [][]int) {
8181
levels = append(levels, level)
8282
}
8383
return res, levels
84-
}
84+
}*/
8585

8686
/*func main() {
8787
var edges []Edge

0 commit comments

Comments
 (0)