Skip to content

Commit 60e61a3

Browse files
author
Eric Lee
committed
add sort test
1 parent 7fecb25 commit 60e61a3

4 files changed

Lines changed: 74 additions & 7 deletions

File tree

‎algorithm/sort/bubble.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package sort
22

33
//冒泡排序,a是数组,n表示数组大小
4-
func BubbleSort(a []int, n int) {
4+
func BubbleSort(a []int) {
5+
n := len(a)
56
if n <= 1 {
67
return
78
}

‎algorithm/sort/bucket.go‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package sort
22

3-
import (
4-
"fmt"
5-
)
6-
73
// 桶排序
84

95
// 获取待排序数组中的最大值
@@ -52,7 +48,6 @@ func BucketSortSimple(source []int) {
5248
for i := 0; i < len(source); i++ {
5349
array[source[i]]++
5450
}
55-
fmt.Println(array)
5651
c := make([]int, 0)
5752
for i := 0; i < len(array); i++ {
5853
for array[i] != 0 {

‎algorithm/sort/counting.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package sort
22

33
import "math"
44

5-
func CountingSort(a []int, n int) {
5+
func CountingSort(a []int) {
6+
n := len(a)
67
if n <= 1 {
78
return
89
}

‎algorithm/sort/sort_test.go‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,71 @@
11
package sort
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestSort(t *testing.T) {
9+
testSlices := [][]int{
10+
{},
11+
{1, 3, 5, 7, 9, 2, 4, 6, 8, 0},
12+
{1, 3, 5, 7, 9, 1, 3, 5, 7, 9},
13+
}
14+
resultSlices := [][]int{
15+
{},
16+
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
17+
{1, 1, 3, 3, 5, 5, 7, 7, 9, 9},
18+
}
19+
20+
for i, testSlice := range testSlices {
21+
testNums := make([]int, len(testSlice))
22+
23+
copy(testNums, testSlice)
24+
BubbleSort(testNums)
25+
if !reflect.DeepEqual(testNums, resultSlices[i]) {
26+
t.Fail()
27+
}
28+
29+
copy(testNums, testSlice)
30+
BucketSort(testNums)
31+
if !reflect.DeepEqual(testNums, resultSlices[i]) {
32+
t.Fail()
33+
}
34+
35+
copy(testNums, testSlice)
36+
BucketSortSimple(testNums)
37+
if !reflect.DeepEqual(testNums, resultSlices[i]) {
38+
t.Fail()
39+
}
40+
41+
copy(testNums, testSlice)
42+
CountingSort(testNums)
43+
if !reflect.DeepEqual(testNums, resultSlices[i]) {
44+
t.Fail()
45+
}
46+
47+
copy(testNums, testSlice)
48+
InsertionSort(testNums, len(testNums))
49+
if !reflect.DeepEqual(testNums, resultSlices[i]) {
50+
t.Fail()
51+
}
52+
53+
copy(testNums, testSlice)
54+
MergeSort(testNums)
55+
if !reflect.DeepEqual(testNums, resultSlices[i]) {
56+
t.Fail()
57+
}
58+
59+
copy(testNums, testSlice)
60+
QuickSort(testNums)
61+
if !reflect.DeepEqual(testNums, resultSlices[i]) {
62+
t.Fail()
63+
}
64+
65+
copy(testNums, testSlice)
66+
SelectionSort(testNums)
67+
if !reflect.DeepEqual(testNums, resultSlices[i]) {
68+
t.Fail()
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)