From caae2794c005e61122dcbf9704c1f83f69db3fab Mon Sep 17 00:00:00 2001 From: LouisFonda Date: Tue, 4 Mar 2025 00:34:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go_solutions/go.mod | 3 +++ go_solutions/main.go | 14 ++++++++++++++ go_solutions/sort/bubble-sort.go | 17 +++++++++++++++++ go_solutions/utils/sort-utils.go | 6 ++++++ 4 files changed, 40 insertions(+) create mode 100644 go_solutions/go.mod create mode 100644 go_solutions/main.go create mode 100644 go_solutions/sort/bubble-sort.go create mode 100644 go_solutions/utils/sort-utils.go 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 +}