Skip to content

Commit fd2da0c

Browse files
author
gongwei
committedJul 31, 2020
algorithm init
0 parents  commit fd2da0c

File tree

5 files changed

+291
-0
lines changed

5 files changed

+291
-0
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

‎go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module algorithm
2+
3+
go 1.14

‎mian.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"reflect"
7+
"strconv"
8+
"time"
9+
10+
selfSort "algorithm/sortAlgorithm"
11+
)
12+
13+
func main() {
14+
testnums := 80000
15+
testArr := createArr(testnums)
16+
// testArr := []int{5, 1, 3, 4, 5, 7, 8, 9, 10, 32, 22, 18, 17, 25, 22, 11, 12, 22, 12, 33, 44, 44}
17+
var re []int
18+
19+
sort := selfSort.NewSortService()
20+
re = sort.Sort.InsertSort(testArr)
21+
fmt.Println(re)
22+
23+
sortValue := reflect.ValueOf(sort.Sort)
24+
sortType := reflect.TypeOf(sort.Sort)
25+
26+
a := reflect.ValueOf(testArr)
27+
// aNums := reflect.ValueOf(testnums)
28+
in := []reflect.Value{a}
29+
// inNums := []reflect.Value{a, aNums}
30+
// ret := sortValue.Method(0).Call(in)
31+
// fmt.Println(sortType.Method(0).Name == "BubbleSort")
32+
// a := sortType.Method(0).
33+
// re := a.Method(testArr)
34+
// for _, method := range sortType.Method {
35+
// fmt.Println(method)
36+
// }
37+
// var re []int
38+
// re = selfSort.BubbleSort(testArr)
39+
for i := 0; i < sortValue.NumMethod(); i++ {
40+
methodName := sortType.Method(i).Name
41+
start := time.Now()
42+
switch methodName {
43+
// case "CountSort":
44+
// sortValue.MethodByName(methodName).Call(inNums)
45+
default:
46+
sortValue.MethodByName(methodName).Call(in)
47+
}
48+
elapsed := time.Since(start)
49+
fmt.Printf("method name : %-16s\t elapsed time: %6s\t\t \n", methodName, elapsed)
50+
}
51+
}
52+
53+
func createArr(nums int) []int {
54+
var arr []int = []int{}
55+
for i := 0; i < 20000; i++ {
56+
randDateString := fmt.Sprintf("%d", rand.New(rand.NewSource(time.Now().UnixNano())).Intn(nums))
57+
randDateInt, _ := strconv.Atoi(randDateString)
58+
arr = append(arr, randDateInt)
59+
}
60+
return arr
61+
62+
}

‎sortAlgorithm/bubbleSort.go

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package sortalgorithm
2+
3+
type Sort interface {
4+
BubbleSort(nums []int) []int
5+
FastSort(nums []int) []int
6+
InsertSort(arr []int) []int
7+
MergeSort(arr []int) []int
8+
HeapSort(arr []int) []int
9+
CountSort(arr []int) []int
10+
}
11+
12+
type sortF struct{}
13+
14+
type SortService struct {
15+
Sort Sort
16+
}
17+
18+
func NewSortService() SortService {
19+
return SortService{
20+
Sort: &sortF{},
21+
}
22+
}
23+
24+
//support BubbleSort
25+
func (s *sortF) BubbleSort(nums []int) []int {
26+
for i := 0; i < len(nums); i++ {
27+
for j := i + 1; j < len(nums); j++ {
28+
if nums[i] <= nums[j] {
29+
continue
30+
} else {
31+
tmp := nums[j]
32+
nums[j] = nums[i]
33+
nums[i] = tmp
34+
}
35+
}
36+
}
37+
return nums
38+
}
39+
40+
func (s *sortF) FastSort(nums []int) []int {
41+
fastSortIter(0, len(nums)-1, nums)
42+
return nums
43+
}
44+
45+
func fastSortIter(left int, right int, nums []int) {
46+
if left < right {
47+
key := nums[left]
48+
i := left
49+
j := right
50+
for i < j {
51+
for i < j && key < nums[j] {
52+
j--
53+
}
54+
if i < j {
55+
nums[i] = nums[j]
56+
i++
57+
}
58+
for i < j && key > nums[i] {
59+
i++
60+
}
61+
if i < j {
62+
nums[j] = nums[i]
63+
j--
64+
}
65+
}
66+
nums[i] = key
67+
fastSortIter(left, i-1, nums)
68+
fastSortIter(i+1, right, nums)
69+
}
70+
}
71+
72+
func (s *sortF) InsertSort(arr []int) []int {
73+
preIndex := 0
74+
curent := 0
75+
for i := 1; i < len(arr); i++ {
76+
curent = arr[i]
77+
preIndex = i - 1
78+
for preIndex >= 0 && arr[preIndex] > curent {
79+
arr[preIndex+1] = arr[preIndex]
80+
preIndex--
81+
}
82+
arr[preIndex+1] = curent
83+
}
84+
return arr
85+
}
86+
87+
func (s *sortF) MergeSort(arr []int) []int {
88+
if len(arr) < 2 {
89+
return arr
90+
}
91+
medium := len(arr) / 2
92+
left := arr[0:medium]
93+
right := arr[medium:]
94+
95+
return merge(s.MergeSort(left), s.MergeSort(right))
96+
97+
}
98+
99+
func merge(left []int, right []int) []int {
100+
var result []int
101+
for len(left) != 0 && len(right) != 0 {
102+
if left[0] < right[0] {
103+
result = append(result, left[0])
104+
left = left[1:]
105+
} else {
106+
result = append(result, right[0])
107+
right = right[1:]
108+
}
109+
}
110+
for len(left) != 0 {
111+
result = append(result, left[0])
112+
left = left[1:]
113+
}
114+
for len(right) != 0 {
115+
result = append(result, right[0])
116+
right = right[1:]
117+
}
118+
return result
119+
}
120+
121+
func (s *sortF) HeapSort(arr []int) []int {
122+
myHeap := newHeap()
123+
for _, a := range arr {
124+
myHeap.insert(a)
125+
}
126+
var re []int = []int{}
127+
for myHeap.N != 0 {
128+
re = append(re, myHeap.pop())
129+
}
130+
return re
131+
}
132+
133+
func (s *sortF) CountSort(arr []int) []int {
134+
max := arr[0]
135+
for i := 0; i < len(arr)-1; i++ {
136+
if arr[i] > max {
137+
max = arr[i]
138+
}
139+
}
140+
tmp := make([]int, max+1)
141+
index := 0
142+
for i := 0; i < len(arr)-1; i++ {
143+
tmp[arr[i]]++
144+
}
145+
var re []int = []int{}
146+
for index <= max {
147+
for tmp[index] > 0 {
148+
re = append(re, index)
149+
tmp[index]--
150+
}
151+
index++
152+
}
153+
return re
154+
}

‎sortAlgorithm/heap.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package sortalgorithm
2+
3+
type Myheap interface {
4+
sink(i int)
5+
swim(i int)
6+
insert(v int)
7+
pop() int
8+
}
9+
10+
type selfHeap struct {
11+
arr []int
12+
N int
13+
}
14+
15+
func newHeap() selfHeap {
16+
N := 0
17+
var arr []int = []int{}
18+
arr = append(arr, 0)
19+
return selfHeap{
20+
arr: arr,
21+
N: N,
22+
}
23+
}
24+
25+
func (h *selfHeap) sink(i int) {
26+
for {
27+
left := 2 * i
28+
right := 2*i + 1
29+
min := left
30+
if left > h.N {
31+
break
32+
}
33+
if left < h.N && h.arr[right] < h.arr[left] {
34+
min = right
35+
}
36+
if right < h.N && h.arr[i] < h.arr[min] {
37+
break
38+
}
39+
h.swap(h.arr, i, min)
40+
i = min
41+
}
42+
43+
}
44+
45+
func (h *selfHeap) swim(i int) {
46+
for i > 1 && h.arr[i] < h.arr[i/2] {
47+
h.swap(h.arr, i, i/2)
48+
i = i / 2
49+
}
50+
}
51+
52+
func (h *selfHeap) swap(nums []int, i, j int) {
53+
nums[i], nums[j] = nums[j], nums[i]
54+
}
55+
56+
func (h *selfHeap) insert(v int) {
57+
h.arr = append(h.arr, v)
58+
h.N++
59+
h.swim(h.N)
60+
}
61+
62+
func (h *selfHeap) pop() int {
63+
if h.N == 0 {
64+
return 0
65+
}
66+
min := h.arr[1]
67+
h.swap(h.arr, 1, h.N)
68+
h.N--
69+
h.sink(1)
70+
return min
71+
}

0 commit comments

Comments
 (0)
Please sign in to comment.