Go

// 冒泡排序算法
bubble_arr := [...]int{24,69,90,57,13}
 
func BubbleSort(arr *[5]int) {
    // 因为数组是值类型,所以修改元素位置是修改数组,所以需要引用传递来实现
    fmt.Println("排序前arr=", (*arr))
    var tmp int // 临时变量用于交换

    for i := 0; i < len(*arr) -1; i ++ {
        
        for j:=0; j < len(*arr) -1 - i; j ++ {
            if (*arr)[j] > (*arr)[j+1] {
                tmp = (*arr)[j]
                (*arr)[j] = (*arr)[j+1]
                (*arr)[j+1] = tmp  
            }
        }
    }

    fmt.Println("排序后arr~~~", (*arr))
}
BubbleSort(&bubble_arr)
fmt.Println(bubble_arr)  // [13 24 57 69 90]
 
 
 
 
原文地址:https://www.cnblogs.com/guo-s/p/14006026.html