Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9319720

Browse files
feat: add sorting algorithms in golang
1 parent 089e19c commit 9319720

File tree

12 files changed

+354
-0
lines changed

12 files changed

+354
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func bubbleSort(nums []int) {
6+
hasChange := true
7+
for i, n := 0, len(nums); i < n-1 && hasChange; i++ {
8+
hasChange = false
9+
for j := 0; j < n-i-1; j++ {
10+
if nums[j] > nums[j+1] {
11+
nums[j], nums[j+1] = nums[j+1], nums[j]
12+
hasChange = true
13+
}
14+
}
15+
}
16+
}
17+
18+
func main() {
19+
nums := []int{1, 2, 7, 9, 5, 8}
20+
bubbleSort(nums)
21+
fmt.Println(nums)
22+
}

‎basic/sorting/BubbleSort/README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,33 @@ let arr = [6, 3, 2, 1, 5];
6666
console.log(bubbleSort(arr))
6767
```
6868

69+
### **Go**
70+
71+
```go
72+
package main
73+
74+
import "fmt"
75+
76+
func bubbleSort(nums []int) {
77+
hasChange := true
78+
for i, n := 0, len(nums); i < n-1 && hasChange; i++ {
79+
hasChange = false
80+
for j := 0; j < n-i-1; j++ {
81+
if nums[j] > nums[j+1] {
82+
nums[j], nums[j+1] = nums[j+1], nums[j]
83+
hasChange = true
84+
}
85+
}
86+
}
87+
}
88+
89+
func main() {
90+
nums := []int{1, 2, 7, 9, 5, 8}
91+
bubbleSort(nums)
92+
fmt.Println(nums)
93+
}
94+
```
95+
6996
<!-- tabs:end -->
7097

7198
## 算法分析
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func insertionSort(nums []int) {
6+
for i, n := 1, len(nums); i < n; i++ {
7+
j, num := i-1, nums[i]
8+
for ; j >= 0 && nums[j] > num; j-- {
9+
nums[j+1] = nums[j]
10+
}
11+
nums[j+1] = num
12+
}
13+
}
14+
15+
func main() {
16+
nums := []int{1, 2, 7, 9, 5, 8}
17+
insertionSort(nums)
18+
fmt.Println(nums)
19+
}

‎basic/sorting/InsertionSort/README.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,30 @@ let arr = [6, 3, 2, 1, 5];
6161
console.log(insertionSort(arr))
6262
```
6363

64+
### **Go**
65+
66+
```go
67+
package main
68+
69+
import "fmt"
70+
71+
func insertionSort(nums []int) {
72+
for i, n := 1, len(nums); i < n; i++ {
73+
j, num := i-1, nums[i]
74+
for ; j >= 0 && nums[j] > num; j-- {
75+
nums[j+1] = nums[j]
76+
}
77+
nums[j+1] = num
78+
}
79+
}
80+
81+
func main() {
82+
nums := []int{1, 2, 7, 9, 5, 8}
83+
insertionSort(nums)
84+
fmt.Println(nums)
85+
}
86+
```
87+
6488
<!-- tabs:end -->
6589

6690
## 算法分析

‎basic/sorting/MergeSort/MergeSort.go‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func merge(nums, temp []int, low, mid, high int) {
6+
for i, j, k := low, mid+1, low; k <= high; k++ {
7+
if j > high || nums[i] < nums[j] {
8+
temp[k] = nums[i]
9+
i++
10+
} else {
11+
temp[k] = nums[j]
12+
j++
13+
}
14+
}
15+
for i := low; i <= high; i++ {
16+
nums[i] = temp[i]
17+
}
18+
}
19+
20+
func _mergeSort(nums, temp []int, low, high int) {
21+
if low >= high {
22+
return
23+
}
24+
mid := low + (high-low)/2
25+
_mergeSort(nums, temp, low, mid)
26+
_mergeSort(nums, temp, mid+1, high)
27+
merge(nums, temp, low, mid, high)
28+
}
29+
30+
func mergeSort(nums []int) {
31+
n := len(nums)
32+
temp := make([]int, n)
33+
_mergeSort(nums, temp, 0, n-1)
34+
}
35+
36+
func main() {
37+
nums := []int{1, 2, 7, 4, 5, 3}
38+
mergeSort(nums)
39+
fmt.Println(nums)
40+
}

‎basic/sorting/MergeSort/README.md‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,51 @@ arr = [3, 5, 6, 2, 1, 7, 4];
8484
console.log(mergeSort(arr));
8585
```
8686

87+
### **Go**
88+
89+
```go
90+
package main
91+
92+
import "fmt"
93+
94+
func merge(nums, temp []int, low, mid, high int) {
95+
for i, j, k := low, mid+1, low; k <= high; k++ {
96+
if j > high || nums[i] < nums[j] {
97+
temp[k] = nums[i]
98+
i++
99+
} else {
100+
temp[k] = nums[j]
101+
j++
102+
}
103+
}
104+
for i := low; i <= high; i++ {
105+
nums[i] = temp[i]
106+
}
107+
}
108+
109+
func _mergeSort(nums, temp []int, low, high int) {
110+
if low >= high {
111+
return
112+
}
113+
mid := low + (high-low)/2
114+
_mergeSort(nums, temp, low, mid)
115+
_mergeSort(nums, temp, mid+1, high)
116+
merge(nums, temp, low, mid, high)
117+
}
118+
119+
func mergeSort(nums []int) {
120+
n := len(nums)
121+
temp := make([]int, n)
122+
_mergeSort(nums, temp, 0, n-1)
123+
}
124+
125+
func main() {
126+
nums := []int{1, 2, 7, 4, 5, 3}
127+
mergeSort(nums)
128+
fmt.Println(nums)
129+
}
130+
```
131+
87132

88133
<!-- tabs:end -->
89134

‎basic/sorting/QuickSort/QuickSort.go‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func partition(nums []int, low, high int) int {
6+
pivot := nums[low]
7+
for low < high {
8+
for low < high && nums[high] >= pivot {
9+
high--
10+
}
11+
nums[low] = nums[high]
12+
for low < high && nums[low] < pivot {
13+
low++
14+
}
15+
nums[high] = nums[low]
16+
}
17+
nums[low] = pivot
18+
return low
19+
}
20+
21+
func _quickSort(nums []int, low, high int) {
22+
if low >= high {
23+
return
24+
}
25+
mid := partition(nums, low, high)
26+
_quickSort(nums, low, mid)
27+
_quickSort(nums, mid+1, high)
28+
}
29+
30+
func quickSort(nums []int) {
31+
_quickSort(nums, 0, len(nums)-1)
32+
}
33+
34+
func main() {
35+
nums := []int{1, 2, 7, 4, 5, 3}
36+
quickSort(nums)
37+
fmt.Println(nums)
38+
}

‎basic/sorting/QuickSort/README.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,49 @@ console.log(quickSort(arr))
9393

9494
```
9595

96+
### **Go**
97+
98+
```go
99+
package main
100+
101+
import "fmt"
102+
103+
func partition(nums []int, low, high int) int {
104+
pivot := nums[low]
105+
for low < high {
106+
for low < high && nums[high] >= pivot {
107+
high--
108+
}
109+
nums[low] = nums[high]
110+
for low < high && nums[low] < pivot {
111+
low++
112+
}
113+
nums[high] = nums[low]
114+
}
115+
nums[low] = pivot
116+
return low
117+
}
118+
119+
func _quickSort(nums []int, low, high int) {
120+
if low >= high {
121+
return
122+
}
123+
mid := partition(nums, low, high)
124+
_quickSort(nums, low, mid)
125+
_quickSort(nums, mid+1, high)
126+
}
127+
128+
func quickSort(nums []int) {
129+
_quickSort(nums, 0, len(nums)-1)
130+
}
131+
132+
func main() {
133+
nums := []int{1, 2, 7, 4, 5, 3}
134+
quickSort(nums)
135+
fmt.Println(nums)
136+
}
137+
```
138+
96139
<!-- tabs:end -->
97140

98141
## 算法分析

‎basic/sorting/SelectionSort/README.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,32 @@ let arr = [6, 3, 2, 1, 5];
6464
console.log(selectionSort(arr))
6565
```
6666

67+
### **Go**
68+
69+
```go
70+
package main
71+
72+
import "fmt"
73+
74+
func selectionSort(nums []int) {
75+
for i, n := 0, len(nums); i < n-1; i++ {
76+
minIndex := i
77+
for j := i + 1; j < n; j++ {
78+
if nums[j] < nums[minIndex] {
79+
minIndex = j
80+
}
81+
}
82+
nums[minIndex], nums[i] = nums[i], nums[minIndex]
83+
}
84+
}
85+
86+
func main() {
87+
nums := []int{1, 2, 7, 9, 5, 8}
88+
selectionSort(nums)
89+
fmt.Println(nums)
90+
}
91+
```
92+
6793
<!-- tabs:end -->
6894

6995
## 算法分析
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func selectionSort(nums []int) {
6+
for i, n := 0, len(nums); i < n-1; i++ {
7+
minIndex := i
8+
for j := i + 1; j < n; j++ {
9+
if nums[j] < nums[minIndex] {
10+
minIndex = j
11+
}
12+
}
13+
nums[minIndex], nums[i] = nums[i], nums[minIndex]
14+
}
15+
}
16+
17+
func main() {
18+
nums := []int{1, 2, 7, 9, 5, 8}
19+
selectionSort(nums)
20+
fmt.Println(nums)
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /