mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 22:28:40 +08:00 
			
		
		
		
	bubble sort using go
This commit is contained in:
		
							
								
								
									
										38
									
								
								codes/go/chapter_sorting/bubble_sort/bubble_sort.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								codes/go/chapter_sorting/bubble_sort/bubble_sort.go
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										40
									
								
								codes/go/chapter_sorting/bubble_sort/bubble_sort_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								codes/go/chapter_sorting/bubble_sort/bubble_sort_test.go
									
									
									
									
									
										Normal file
									
								
							@ -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)
 | 
			
		||||
}
 | 
			
		||||
@ -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"
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user