Skip to content

Commit a7e8f47

Browse files
committed
增加冒泡排序
1 parent a1e0d92 commit a7e8f47

File tree

5 files changed

+81
-16
lines changed

5 files changed

+81
-16
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#bench gen
2+
*/*.test
3+
*/*.profile
4+
*/*.out

bubble/bubble.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//冒泡排序
2+
package bubble
3+
4+
//原理:重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成
5+
6+
//算法步骤:
7+
//1. 比较相邻的元素。如果第一个比第二个大,就交换他们两个。
8+
//2. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。这步做完后,最后的元素会是最大的数。
9+
//3. 针对所有的元素重复以上的步骤,除了最后一个。
10+
//4. 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。
11+
12+
func BubbleSort(arr []int) []int {
13+
length := len(arr) //获取最大长度
14+
for i := 0; i < length; i++ { //比较次数
15+
for j := 0; j < length-1-i; j++ { //比较长度
16+
if arr[j] > arr[j+1] {
17+
arr[j], arr[j+1] = arr[j+1], arr[j]
18+
}
19+
}
20+
}
21+
return arr
22+
}

bubble/bubble_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package bubble
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
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+
)
19+
20+
func TestBubbleSort(t *testing.T) {
21+
for _, v := range values {
22+
assert.Exactly(t, v.sort, BubbleSort(v.nosort), "no eq")
23+
}
24+
}
25+
26+
func BenchmarkBubbleSort(b *testing.B) {
27+
b.ReportAllocs()
28+
for i := 0; i < b.N; i++ {
29+
BubbleSort(value)
30+
}
31+
}

insertion/insertion.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@ package insertion
77
// 1.第一待排序序列第一个元素看做一个有序序列,把第二个元素到最后一个元素当成是未排序序列
88
// 2.从头到尾依次扫描未排序序列,将扫描到的每个元素插入有序序列的适当位置。(如果待插入的元素与有序序列中的某个元素相等,则将待插入元素插入到相等元素的后面
99

10-
11-
//[]int{3,44,56,38,77,38,26}
1210
//[]int{3}
11+
//[]int{3,44,56,38,77,38,26}
1312

1413
func InsertionSort(arr []int) []int {
1514
for i := range arr {
16-
preIndex := i -1
15+
preIndex := i - 1
1716
current := arr[i]
18-
for preIndex >=0 && arr[preIndex] > current{
19-
arr[preIndex + 1] = arr[preIndex]
20-
preIndex -=1
17+
for preIndex >= 0 && arr[preIndex] > current {
18+
arr[preIndex+1] = arr[preIndex]
19+
preIndex -= 1
2120
}
22-
arr[preIndex +1] = current
21+
arr[preIndex+1] = current
2322
}
2423
return arr
25-
}
24+
}

insertion/insertion_test.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
package insertion
22

33
import (
4-
"testing"
54
"github.com/stretchr/testify/assert"
5+
"testing"
66
)
77

88
var (
9-
insertValues = []struct {
9+
values = []struct {
1010
nosort []int
11-
sort []int
11+
sort []int
1212
}{
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}},
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}},
1616
}
17+
value = []int{3, 44, 56, 38, 77, 38, 26}
1718
)
19+
1820
func TestInsertionSort(t *testing.T) {
19-
for _,v := range insertValues {
20-
assert.Exactly(t,v.sort,InsertionSort(v.nosort),"no eq")
21+
for _, v := range values {
22+
assert.Exactly(t, v.sort, InsertionSort(v.nosort), "no eq")
23+
}
24+
}
25+
26+
func BenchmarkInsertionSort(b *testing.B) {
27+
b.ReportAllocs()
28+
for i := 0; i < b.N; i++ {
29+
InsertionSort(value)
2130
}
2231
}

0 commit comments

Comments
 (0)