Skip to content

Commit 8031520

Browse files
committed
<go-implement><quicksort & mergesort>
implement insertsort and selectsort and shellsort in go version signed-off-by: ray1888
1 parent f7673ac commit 8031520

File tree

10 files changed

+152
-9
lines changed

10 files changed

+152
-9
lines changed

go/src/algocasts/insertsort.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package algocasts
2+
3+
func insertsort(input []int) {
4+
if input == nil || len(input) == 0 {
5+
return
6+
}
7+
for i := 1; i < len(input); i++ {
8+
cur := input[i]
9+
j := i - 1
10+
for j >= 0 && input[j] > cur {
11+
input[j+1] = input[j]
12+
j -= 1
13+
}
14+
input[j+1] = cur
15+
}
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package algocasts
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestInsertSort(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
input []int
12+
want []int
13+
}{
14+
{"double", []int{2, 1}, []int{1, 2}},
15+
{"single", []int{1, 5, 4}, []int{1, 4, 5}},
16+
{"normal", []int{1, 9, 5, 3, 2, 6}, []int{1, 2, 3, 5, 6, 9}},
17+
}
18+
for _, tt := range tests {
19+
insertsort(tt.input)
20+
assert.Equal(t, tt.input, tt.want)
21+
}
22+
}

go/src/algocasts/mergesort_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ func TestMergeSortRecursive(t *testing.T) {
1717
}
1818
for _, tt := range tests {
1919
MergeSortRecursive(tt.input)
20-
t.Log(tt.input, tt.want)
2120
assert.Equal(t, tt.input, tt.want)
2221
}
2322
}

go/src/algocasts/quicksort.go

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

3-
func swap(array []int, a int, b int) {
4-
tmp := array[b]
5-
array[b] = array[a]
6-
array[a] = tmp
7-
}
8-
93
func lomutopartition(input []int, low, high int) int {
104
pivot := input[high]
115
i := low

go/src/algocasts/quicksort_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ func TestQuickSort(t *testing.T) {
1717
}
1818
for _, tt := range tests {
1919
Quicksort(tt.input)
20-
t.Log(tt.input, tt.want)
2120
assert.Equal(t, tt.input, tt.want)
2221
}
2322
}
@@ -34,7 +33,6 @@ func TestLomutoSort(t *testing.T) {
3433
}
3534
for _, tt := range tests {
3635
Lomutosort(tt.input)
37-
t.Log(tt.input, tt.want)
3836
assert.Equal(t, tt.input, tt.want)
3937
}
4038
}

go/src/algocasts/selectsort.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package algocasts
2+
3+
func selectsortMin(input []int) {
4+
if input == nil || len(input) == 0 {
5+
return
6+
}
7+
for i := 0; i < len(input); i++ {
8+
minId := i
9+
for j := i + 1; j < len(input); j++ {
10+
if input[j] < input[minId] {
11+
minId = j
12+
}
13+
}
14+
swap(input, i, minId)
15+
}
16+
}
17+
18+
func selectsortMax(input []int) {
19+
if input == nil || len(input) == 0 {
20+
return
21+
}
22+
for i := len(input) - 1; i >= 0; i-- {
23+
maxId := i
24+
for j := 0; j < i; j++ {
25+
if input[j] > input[maxId] {
26+
maxId = j
27+
}
28+
}
29+
swap(input, i, maxId)
30+
}
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package algocasts
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestSelectSortMin(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
input []int
12+
want []int
13+
}{
14+
{"single", []int{1, 5, 4}, []int{1, 4, 5}},
15+
{"normal", []int{1, 9, 5, 3, 2, 6}, []int{1, 2, 3, 5, 6, 9}},
16+
}
17+
for _, tt := range tests {
18+
selectsortMin(tt.input)
19+
assert.Equal(t, tt.input, tt.want)
20+
}
21+
}
22+
23+
func TestSelectSortMax(t *testing.T) {
24+
tests := []struct {
25+
name string
26+
input []int
27+
want []int
28+
}{
29+
{"single", []int{1, 5, 4}, []int{1, 4, 5}},
30+
{"normal", []int{1, 9, 5, 3, 2, 6}, []int{1, 2, 3, 5, 6, 9}},
31+
}
32+
for _, tt := range tests {
33+
selectsortMax(tt.input)
34+
assert.Equal(t, tt.input, tt.want)
35+
}
36+
}

go/src/algocasts/shellsort.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package algocasts
2+
3+
func shellsort(input []int) {
4+
if input == nil || len(input) == 0 {
5+
return
6+
}
7+
// >>= means bit counting left move
8+
for gap := len(input) >> 1; gap > 0; gap >>= 1 {
9+
for i := gap; i < len(input); i++ {
10+
cur := input[i]
11+
j := i - gap
12+
for j >= 0 && input[j] > cur {
13+
input[j+gap] = input[j]
14+
j -= gap
15+
}
16+
input[j+gap] = cur
17+
}
18+
}
19+
}

go/src/algocasts/shellsort_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package algocasts
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestShellSort(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
input []int
12+
want []int
13+
}{
14+
{"single", []int{1, 5, 4}, []int{1, 4, 5}},
15+
{"normal", []int{1, 9, 5, 3, 2, 6}, []int{1, 2, 3, 5, 6, 9}},
16+
}
17+
for _, tt := range tests {
18+
shellsort(tt.input)
19+
assert.Equal(t, tt.input, tt.want)
20+
}
21+
}

go/src/algocasts/util.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package algocasts
2+
3+
func swap(array []int, a int, b int) {
4+
tmp := array[b]
5+
array[b] = array[a]
6+
array[a] = tmp
7+
}

0 commit comments

Comments
 (0)