Skip to content

Commit d504855

Browse files
author
mattan
committed
- fixed test names
- moved selection sort test and benchmark to the end because it takes very long to finish.
1 parent 82ff0c0 commit d504855

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

sorts/selection_sort.go

Lines changed: 1 addition & 1 deletion
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-
func selectionSort(arr []int) []int {
4+
func SelectionSort(arr []int) []int {
55

66
for i := 0; i < len(arr); i++ {
77
min := i

sorts/sorts_case_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,36 @@ var arr []int = makeRandomSignedSlice(500_000)
1919
var sortTests = []sortTest{
2020
//Sorted slice
2121
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
22-
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "Sorted Signed"},
22+
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "Sorted Unsigned"},
2323
//Reversed slice
2424
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
25-
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "Reversed Signed"},
26-
27-
//500k int values sort
28-
{arr, getSortedVersion(arr), "Large Random Signed"},
25+
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "Reversed Unsigned"},
26+
//500k unsigned int values sort
27+
{uarr, getSortedVersion(uarr), "Large Random Unsigned"},
2928

3029
//Sorted slice
3130
{[]int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
32-
[]int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "Sorted Unsigned"},
31+
[]int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "Sorted Signed"},
3332

3433
//Reversed slice
3534
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10},
36-
[]int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "Reversed Unsigned"},
35+
[]int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "Reversed Signed"},
3736

3837
//Reversed slice, even length
3938
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10},
40-
[]int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "Reversed Unsigned #2"},
39+
[]int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "Reversed Signed #2"},
4140

4241
//Random order with repetitions
4342
{[]int{-5, 7, 4, -2, 6, 5, 8, 3, 2, -7, -1, 0, -3, 9, -6, -4, 10, 9, 1, -8, -9, -10},
44-
[]int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10}, "Random order"},
43+
[]int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10}, "Random order Signed"},
44+
45+
//500k int values sort
46+
{arr, getSortedVersion(arr), "Large Random Signed"},
4547

4648
//Empty slice
4749
{[]int{}, []int{}, "Empty"},
4850
//Single-entry slice
4951
{[]int{1}, []int{1}, "Singleton"},
50-
51-
//500k non negative int values sort
52-
{uarr, getSortedVersion(uarr), "Large Random unsigned"},
5352
}
5453

5554
func makeRandomUnsignedSlice(size int) []int {

sorts/sorts_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package sorts
22

33
import (
4-
"fmt"
54
"testing"
65
)
76

@@ -14,12 +13,7 @@ func testFramework(t *testing.T, sortingFunction func([]int) []int) {
1413
if pos == -1 {
1514
t.Errorf("test %s failed due to slice length changing", test.name)
1615
}
17-
18-
for i := pos - 10; i < pos+10; i++ {
19-
fmt.Print(actual[i], " ")
20-
}
2116
t.Errorf("test %s failed at index %d", test.name, pos)
22-
2317
}
2418
})
2519
}
@@ -55,6 +49,11 @@ func TestRadix(t *testing.T) {
5549
testFramework(t, RadixSort)
5650
}
5751

52+
// Very slow, consider commenting
53+
func TestSelection(t *testing.T) {
54+
testFramework(t, SelectionSort)
55+
}
56+
5857
/* func TestTopological(t *testing.T) {
5958
testFramework(t, topologicalSort)
6059
} */
@@ -70,14 +69,6 @@ func BenchmarkBubble(b *testing.B) {
7069
}
7170
}
7271

73-
func BenchmarkSelection(b *testing.B) {
74-
for i := 0; i < b.N; i++ {
75-
for _, test := range sortTests {
76-
selectionSort(test.input)
77-
}
78-
}
79-
}
80-
8172
func BenchmarkInsertion(b *testing.B) {
8273
for i := 0; i < b.N; i++ {
8374
for _, test := range sortTests {
@@ -126,6 +117,15 @@ func BenchmarkRadix(b *testing.B) {
126117
}
127118
}
128119

120+
// Very Slow, consider commenting
121+
func BenchmarkSelection(b *testing.B) {
122+
for i := 0; i < b.N; i++ {
123+
for _, test := range sortTests {
124+
SelectionSort(test.input)
125+
}
126+
}
127+
}
128+
129129
/*func BenchmarkTopological(b *testing.B) {
130130
for i := 0; i < b.N; i++ {
131131
for _, test := range sortTests {

0 commit comments

Comments
 (0)