From 10b1216675a8be31ea99ab7c25dc7ab4153d7ef5 Mon Sep 17 00:00:00 2001 From: chenshilong Date: Tue, 6 Dec 2022 23:41:12 +0800 Subject: [PATCH 1/2] bubble sort using go --- .../bubble_sort/bubble_sort.go | 38 ++++++++++++++++++ .../bubble_sort/bubble_sort_test.go | 40 +++++++++++++++++++ docs/chapter_sorting/bubble_sort.md | 33 ++++++++++++++- 3 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 codes/go/chapter_sorting/bubble_sort/bubble_sort.go create mode 100644 codes/go/chapter_sorting/bubble_sort/bubble_sort_test.go diff --git a/codes/go/chapter_sorting/bubble_sort/bubble_sort.go b/codes/go/chapter_sorting/bubble_sort/bubble_sort.go new file mode 100644 index 000000000..a51d19925 --- /dev/null +++ b/codes/go/chapter_sorting/bubble_sort/bubble_sort.go @@ -0,0 +1,38 @@ +// File: bubble_sort.go +// Created Time: 2022-12-06 +// Author: Slone123c (274325721@qq.com) + +package bubble_sort + +/* 冒泡排序 */ +func bubbleSort(nums []int) { + // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + for i := len(nums) - 1; i > 0; i-- { + // 内循环:冒泡操作 + for j := 0; j < i; j++ { + if nums[j] > nums[j+1] { + // 交换 nums[j] 与 nums[j + 1] + nums[j], nums[j+1] = nums[j+1], nums[j] + } + } + } +} + +/* 冒泡排序(标志优化)*/ +func bubbleSortWithFlag(nums []int) { + // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + for i := len(nums) - 1; i > 0; i-- { + flag := false // 初始化标志位 + // 内循环:冒泡操作 + for j := 0; j < i; j++ { + if nums[j] > nums[j+1] { + // 交换 nums[j] 与 nums[j + 1] + nums[j], nums[j+1] = nums[j+1], nums[j] + flag = true // 记录交换元素 + } + } + if flag == false { // 此轮冒泡未交换任何元素,直接跳出 + break + } + } +} diff --git a/codes/go/chapter_sorting/bubble_sort/bubble_sort_test.go b/codes/go/chapter_sorting/bubble_sort/bubble_sort_test.go new file mode 100644 index 000000000..51b8915d2 --- /dev/null +++ b/codes/go/chapter_sorting/bubble_sort/bubble_sort_test.go @@ -0,0 +1,40 @@ +// File: bubble_sort_test.go +// Created Time: 2022-12-06 +// Author: Slone123c (274325721@qq.com) + +package bubble_sort + +import ( + "fmt" + "testing" +) + +func TestBubbleSort(t *testing.T) { + var ( + arr = []int{5, 4, 3, 2, 1} + ) + fmt.Println("冒泡排序前的数组:", arr) + bubbleSort(arr) + for i := 1; i < len(arr); i++ { + if arr[i] < arr[i-1] { + t.Errorf("排序不正确") + break + } + } + fmt.Println("冒泡排序后的数组:", arr) +} + +func TestBubbleSortWithFlag(t *testing.T) { + var ( + arr = []int{5, 4, 3, 2, 1} + ) + fmt.Println("冒泡排序前的数组:", arr) + bubbleSortWithFlag(arr) + for i := 1; i < len(arr); i++ { + if arr[i] < arr[i-1] { + t.Errorf("排序不正确") + break + } + } + fmt.Println("冒泡排序后的数组:", arr) +} diff --git a/docs/chapter_sorting/bubble_sort.md b/docs/chapter_sorting/bubble_sort.md index 05ef478b0..116185028 100644 --- a/docs/chapter_sorting/bubble_sort.md +++ b/docs/chapter_sorting/bubble_sort.md @@ -112,7 +112,19 @@ comments: true === "Go" ```go title="bubble_sort.go" - + /* 冒泡排序 */ + func bubbleSort(nums []int) { + // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + for i := len(nums) - 1; i > 0; i-- { + // 内循环:冒泡操作 + for j := 0; j < i; j++ { + if nums[j] > nums[j+1] { + // 交换 nums[j] 与 nums[j + 1] + nums[j], nums[j+1] = nums[j+1], nums[j] + } + } + } + } ``` === "JavaScript" @@ -239,7 +251,24 @@ comments: true === "Go" ```go title="bubble_sort.go" - + /* 冒泡排序(标志优化)*/ + func bubbleSortWithFlag(nums []int) { + // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + for i := len(nums) - 1; i > 0; i-- { + flag := false // 初始化标志位 + // 内循环:冒泡操作 + for j := 0; j < i; j++ { + if nums[j] > nums[j+1] { + // 交换 nums[j] 与 nums[j + 1] + nums[j], nums[j+1] = nums[j+1], nums[j] + flag = true // 记录交换元素 + } + } + if flag == false { // 此轮冒泡未交换任何元素,直接跳出 + break + } + } + } ``` === "JavaScript" From 8643efece96023cec959d24df6856491c4043ac9 Mon Sep 17 00:00:00 2001 From: chenshilong Date: Wed, 7 Dec 2022 18:38:12 +0800 Subject: [PATCH 2/2] 1.bubble sort using go 2.test edited --- .../bubble_sort/bubble_sort_test.go | 32 ++++--------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/codes/go/chapter_sorting/bubble_sort/bubble_sort_test.go b/codes/go/chapter_sorting/bubble_sort/bubble_sort_test.go index 51b8915d2..bab85139e 100644 --- a/codes/go/chapter_sorting/bubble_sort/bubble_sort_test.go +++ b/codes/go/chapter_sorting/bubble_sort/bubble_sort_test.go @@ -10,31 +10,11 @@ import ( ) func TestBubbleSort(t *testing.T) { - var ( - arr = []int{5, 4, 3, 2, 1} - ) - fmt.Println("冒泡排序前的数组:", arr) - bubbleSort(arr) - for i := 1; i < len(arr); i++ { - if arr[i] < arr[i-1] { - t.Errorf("排序不正确") - break - } - } - fmt.Println("冒泡排序后的数组:", arr) -} + nums := []int{4, 1, 3, 1, 5, 2} + bubbleSort(nums) + fmt.Println("冒泡排序完成后 nums = ", nums) -func TestBubbleSortWithFlag(t *testing.T) { - var ( - arr = []int{5, 4, 3, 2, 1} - ) - fmt.Println("冒泡排序前的数组:", arr) - bubbleSortWithFlag(arr) - for i := 1; i < len(arr); i++ { - if arr[i] < arr[i-1] { - t.Errorf("排序不正确") - break - } - } - fmt.Println("冒泡排序后的数组:", arr) + nums1 := []int{4, 1, 3, 1, 5, 2} + bubbleSortWithFlag(nums1) + fmt.Println("冒泡排序完成后 nums1 = ", nums) }