美文网首页
go语言实现冒泡排序

go语言实现冒泡排序

作者: 胤熙 | 来源:发表于2020-07-08 11:41 被阅读0次
    package main
    
    import "fmt"
    
    func main()  {
        println("冒泡排序")
        testArr := []int{23,12,34,6,8,342,234,8677,33,22,88}
        fmt.Println("排序前",testArr)
        bubbleSort2(testArr)
        fmt.Println("排序后",testArr)
    }
    
    //冒泡排序
    func bubbleSort(needSortArr []int){
        arrLength := len(needSortArr)
        for i := 0;i < arrLength - 1; i++ {
            for k := 0; k < arrLength  - 1 - i; k++ {
                if needSortArr[k] > needSortArr[k+1] {
                    needSortArr[k] = needSortArr[k] + needSortArr[k+1]
                    needSortArr[k+1] = needSortArr[k] - needSortArr[k+1]
                    needSortArr[k] = needSortArr[k] - needSortArr[k+1]
                }
            }
        }
    }
    
    //冒泡排序
    func bubbleSort2(needSortArr []int){
        arrLength := len(needSortArr)
        for i := 0;i < arrLength - 1; i++ {
            for k := 0; k < arrLength  - 1 - i; k++ {
                if needSortArr[k] > needSortArr[k+1] {
                    needSortArr[k] , needSortArr[k+1] = needSortArr[k+1] , needSortArr[k]
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:go语言实现冒泡排序

          本文链接:https://www.haomeiwen.com/subject/ctgqcktx.html