Skip to content

Commit 8a0f7f7

Browse files
authored
init go implementation
2 parents 237cc0e + 8031520 commit 8a0f7f7

File tree

15 files changed

+396
-0
lines changed

15 files changed

+396
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
.idea
33
*.iml
44
target
5+
6+
# go
7+
go/.idea
8+

go/Readme.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Algocasts sorting-algorithem(Go version)
2+
3+
4+
## usage
5+
6+
### precondition
7+
1. go version >= 1.11
8+
2. go mod is enabled (if not enable, google how to enable )
9+
10+
### usage
11+
1. go mod download
12+
2. use cmd like this to run test
13+
14+
(using go mod somehow can't use goland test button to run test , this problem will be solve later)
15+
```
16+
go test mergesort_test.go mergesort.go
17+
```
18+
19+
20+
21+

go/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module sorting-algorithems
2+
3+
go 1.12
4+
5+
require github.com/stretchr/testify v1.3.0

go/go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
7+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

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.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package algocasts
2+
3+
func merge(array []int, low int, mid int, high int, tmp []int) {
4+
var i, j, k int = low, mid + 1, 0
5+
for i <= mid && j <= high {
6+
if array[i] > array[j] {
7+
tmp[k] = array[j]
8+
k += 1
9+
j += 1
10+
} else {
11+
tmp[k] = array[i]
12+
k += 1
13+
i += 1
14+
}
15+
}
16+
for i <= mid {
17+
tmp[k] = array[i]
18+
k += 1
19+
i += 1
20+
}
21+
for j <= high {
22+
tmp[k] = array[j]
23+
k += 1
24+
j += 1
25+
}
26+
copy(array[low:low+k], tmp[0:k])
27+
}
28+
29+
func mergeSort(array []int, low int, high int, tmp []int) {
30+
if low < high {
31+
mid := low + (high-low)/2
32+
mergeSort(array, low, mid, tmp)
33+
mergeSort(array, mid+1, high, tmp)
34+
merge(array, low, mid, high, tmp)
35+
}
36+
}
37+
38+
func MergeSortRecursive(input []int) {
39+
if input == nil || len(input) == 0 {
40+
return
41+
}
42+
tmp := make([]int, len(input))
43+
mergeSort(input, 0, len(input)-1, tmp)
44+
}
45+
46+
func min(a, b int) int {
47+
if a > b {
48+
return b
49+
} else {
50+
return a
51+
}
52+
}
53+
54+
func mergerSortIterative(input []int) {
55+
if input == nil || len(input) == 0 {
56+
return
57+
}
58+
var length int
59+
tmp := make([]int, len(input))
60+
for length = 1; length < len(input); length *= 2 {
61+
for low := 0; low < len(input); low += 2 * length {
62+
mid := min(low+length-1, len(input)-1)
63+
high := min(low+2*length-1, len(input)-1)
64+
merge(input, low, mid, high, tmp)
65+
}
66+
}
67+
}

go/src/algocasts/mergesort_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package algocasts
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestMergeSortRecursive(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+
MergeSortRecursive(tt.input)
20+
assert.Equal(t, tt.input, tt.want)
21+
}
22+
}
23+
24+
func TestMergeSortIterative(t *testing.T) {
25+
tests := []struct {
26+
name string
27+
input []int
28+
want []int
29+
}{
30+
{"double", []int{2, 1}, []int{1, 2}},
31+
{"single", []int{1, 5, 4}, []int{1, 4, 5}},
32+
{"normal", []int{1, 9, 5, 3, 2, 6}, []int{1, 2, 3, 5, 6, 9}},
33+
}
34+
for _, tt := range tests {
35+
mergerSortIterative(tt.input)
36+
assert.Equal(t, tt.input, tt.want)
37+
}
38+
}

go/src/algocasts/quicksort.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package algocasts
2+
3+
func lomutopartition(input []int, low, high int) int {
4+
pivot := input[high]
5+
i := low
6+
for j := low; j <= high; j++ {
7+
if input[j] < pivot {
8+
swap(input, i, j)
9+
i += 1
10+
}
11+
}
12+
swap(input, i, high)
13+
return i
14+
}
15+
16+
func lomutosort(input []int, low, high int) {
17+
if low < high {
18+
k := lomutopartition(input, low, high)
19+
lomutosort(input, low, k-1)
20+
lomutosort(input, k+1, high)
21+
}
22+
}
23+
24+
func Lomutosort(input []int) {
25+
if input == nil || len(input) == 0 {
26+
return
27+
}
28+
lomutosort(input, 0, len(input)-1)
29+
}
30+
31+
func partition(input []int, low, high int) int {
32+
mid := low + (high-low)/2
33+
pivot := input[mid]
34+
var i, j = low, high
35+
for true {
36+
for input[i] < pivot {
37+
i += 1
38+
}
39+
for input[j] > pivot {
40+
j -= 1
41+
}
42+
if i >= j {
43+
return j
44+
}
45+
swap(input, i, j)
46+
}
47+
return j
48+
}
49+
50+
func quicksort(input []int, low, high int) {
51+
if low >= high {
52+
return
53+
}
54+
k := partition(input, low, high)
55+
quicksort(input, low, k)
56+
quicksort(input, k+1, high)
57+
}
58+
59+
func Quicksort(input []int) {
60+
if input == nil || len(input) == 0 {
61+
return
62+
}
63+
quicksort(input, 0, len(input)-1)
64+
}

go/src/algocasts/quicksort_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package algocasts
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestQuickSort(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+
Quicksort(tt.input)
20+
assert.Equal(t, tt.input, tt.want)
21+
}
22+
}
23+
24+
func TestLomutoSort(t *testing.T) {
25+
tests := []struct {
26+
name string
27+
input []int
28+
want []int
29+
}{
30+
{"double", []int{2, 1}, []int{1, 2}},
31+
{"single", []int{1, 5, 4}, []int{1, 4, 5}},
32+
{"normal", []int{1, 9, 5, 3, 2, 6}, []int{1, 2, 3, 5, 6, 9}},
33+
}
34+
for _, tt := range tests {
35+
Lomutosort(tt.input)
36+
assert.Equal(t, tt.input, tt.want)
37+
}
38+
}

0 commit comments

Comments
 (0)