diff --git a/go_solutions/go.mod b/go_solutions/go.mod new file mode 100644 index 0000000..13bea11 --- /dev/null +++ b/go_solutions/go.mod @@ -0,0 +1,3 @@ +module git.icoding.fun/yigencong/algorithm/go_solutions + +go 1.23.2 diff --git a/go_solutions/main.go b/go_solutions/main.go new file mode 100644 index 0000000..2bf778c --- /dev/null +++ b/go_solutions/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + + "git.icoding.fun/yigencong/algorithm/go_solutions/sort" +) + +func main() { + arr := []int{64, 34, 25, 12, 22, 11, 90} + fmt.Println("原始数组:", arr) + sort.BubbleSort(arr) + fmt.Println("排序后数组:", arr) +} diff --git a/go_solutions/sort/bubble-sort.go b/go_solutions/sort/bubble-sort.go new file mode 100644 index 0000000..45a3235 --- /dev/null +++ b/go_solutions/sort/bubble-sort.go @@ -0,0 +1,17 @@ +package sort + +import ( + "git.icoding.fun/yigencong/algorithm/go_solutions/utils" +) + +// BubbleSort 冒泡排序算法 +func BubbleSort(arr []int) { + n := len(arr) + for i := 0; i < n-1; i++ { + for j := 0; j < n-i-1; j++ { + if arr[j] > arr[j+1] { + utils.Swap(&arr[j], &arr[j+1]) // 使用 Swap 函数交换元素 + } + } + } +} diff --git a/go_solutions/utils/sort-utils.go b/go_solutions/utils/sort-utils.go new file mode 100644 index 0000000..a932d0f --- /dev/null +++ b/go_solutions/utils/sort-utils.go @@ -0,0 +1,6 @@ +package utils + +// 交换数组两个位置的元素 +func Swap(a, b *int) { + *a, *b = *b, *a +}