Skip to content

Commit eb789e8

Browse files
committed
增加选择排序
1 parent a7e8f47 commit eb789e8

File tree

6 files changed

+72
-29
lines changed

6 files changed

+72
-29
lines changed

bubble/bubble_test.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,19 @@ package bubble
33
import (
44
"github.com/stretchr/testify/assert"
55
"testing"
6+
"github.com/xiaomeng79/go-algorithm/testdata"
67
)
78

8-
var (
9-
values = []struct {
10-
nosort []int
11-
sort []int
12-
}{
13-
{[]int{3, 44, 56, 38, 77, 38, 26}, []int{3, 26, 38, 38, 44, 56, 77}},
14-
{[]int{3}, []int{3}},
15-
{[]int{3, -1, -6, 34, -78}, []int{-78, -6, -1, 3, 34}},
16-
}
17-
value = []int{3, 44, 56, 38, 77, 38, 26}
18-
)
199

2010
func TestBubbleSort(t *testing.T) {
21-
for _, v := range values {
22-
assert.Exactly(t, v.sort, BubbleSort(v.nosort), "no eq")
11+
for _, v := range testdata.Values {
12+
assert.Exactly(t, v.Sort, BubbleSort(v.Nosort), "no eq")
2313
}
2414
}
2515

2616
func BenchmarkBubbleSort(b *testing.B) {
2717
b.ReportAllocs()
2818
for i := 0; i < b.N; i++ {
29-
BubbleSort(value)
19+
BubbleSort(testdata.Value)
3020
}
3121
}

insertion/insertion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
package insertion
33

44
//原理:通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入
5-
5+
//稳定性:稳定
66
//算法步骤:
77
// 1.第一待排序序列第一个元素看做一个有序序列,把第二个元素到最后一个元素当成是未排序序列
88
// 2.从头到尾依次扫描未排序序列,将扫描到的每个元素插入有序序列的适当位置。(如果待插入的元素与有序序列中的某个元素相等,则将待插入元素插入到相等元素的后面

insertion/insertion_test.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,19 @@ package insertion
33
import (
44
"github.com/stretchr/testify/assert"
55
"testing"
6+
"github.com/xiaomeng79/go-algorithm/testdata"
67
)
78

8-
var (
9-
values = []struct {
10-
nosort []int
11-
sort []int
12-
}{
13-
{[]int{3, 44, 56, 38, 77, 38, 26}, []int{3, 26, 38, 38, 44, 56, 77}},
14-
{[]int{3}, []int{3}},
15-
{[]int{3, -1, -6, 34, -78}, []int{-78, -6, -1, 3, 34}},
16-
}
17-
value = []int{3, 44, 56, 38, 77, 38, 26}
18-
)
199

2010
func TestInsertionSort(t *testing.T) {
21-
for _, v := range values {
22-
assert.Exactly(t, v.sort, InsertionSort(v.nosort), "no eq")
11+
for _, v := range testdata.Values {
12+
assert.Exactly(t, v.Sort, InsertionSort(v.Nosort), "no eq")
2313
}
2414
}
2515

2616
func BenchmarkInsertionSort(b *testing.B) {
2717
b.ReportAllocs()
2818
for i := 0; i < b.N; i++ {
29-
InsertionSort(value)
19+
InsertionSort(testdata.Value)
3020
}
3121
}

selection/selection.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//选择排序
2+
package selection
3+
4+
//原理:每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置
5+
//稳定: 不稳定
6+
//适用: 数据规模越小越好
7+
//算法步骤:
8+
//1. 首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置
9+
//2. 再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾
10+
//3. 重复第二步,直到所有元素均排序完毕
11+
12+
func SelectionSort(arr []int) []int {
13+
length := len(arr)
14+
for i:= 0;i < length-1;i++ {//注意这里使用length-1少一次,
15+
minIndex := i
16+
for j:= i+1;j<length;j++ {
17+
if arr[j] < arr[minIndex] {
18+
minIndex = j
19+
}
20+
}
21+
//
22+
arr[minIndex],arr[i] = arr[i],arr[minIndex]
23+
}
24+
return arr
25+
}
26+
27+
28+

selection/selection_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package selection
2+
3+
import (
4+
"testing"
5+
"github.com/stretchr/testify/assert"
6+
"github.com/xiaomeng79/go-algorithm/testdata"
7+
)
8+
9+
func TestSelectionSort(t *testing.T) {
10+
for _, v := range testdata.Values {
11+
assert.Exactly(t, v.Sort, SelectionSort(v.Nosort), "no eq")
12+
}
13+
}
14+
15+
16+
func BenchmarkSelectionSort(b *testing.B) {
17+
b.ReportAllocs()
18+
for i:=0;i<b.N;i++ {
19+
SelectionSort(testdata.Value)
20+
}
21+
}
22+

testdata/sortdata.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package testdata
2+
3+
var (
4+
Values = []struct {
5+
Nosort []int
6+
Sort []int
7+
}{
8+
{[]int{3, 44, 56, 38, 77, 38, 26}, []int{3, 26, 38, 38, 44, 56, 77}},
9+
{[]int{3}, []int{3}},
10+
{[]int{3, -1, -6, 34, -78}, []int{-78, -6, -1, 3, 34}},
11+
}
12+
Value = []int{3, 44, 56, 38, 77, 38, 26}
13+
)

0 commit comments

Comments
 (0)