Skip to content

Commit 729ceed

Browse files
committed
add bubble sort
0 parents  commit 729ceed

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## go-algorithm
2+
3+
## Features
4+
[ ]

bubble_sort.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package algorithm
2+
3+
// BubbleSort ..
4+
func BubbleSort(arr []int) []int {
5+
len := len(arr)
6+
for i := 0; i < len-1; i++ {
7+
for j := 0; j < len-1-i; j++ {
8+
if arr[j] > arr[j+1] {
9+
arr[j], arr[j+1] = arr[j+1], arr[j]
10+
}
11+
}
12+
}
13+
return arr
14+
}

bubble_sort_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package algorithm
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestBubbleSort(t *testing.T) {
11+
arr := []int{3, 2, 0, 5, 10, 1}
12+
arrSort := BubbleSort(arr)
13+
fmt.Printf("arr after sored: %v\n", arrSort)
14+
assert.Equal(t, 10, arrSort[5])
15+
}

docs/1.bubble_sort.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# 冒泡排序

0 commit comments

Comments
 (0)