zk90

导航

golang实现冒泡排序

//BubbleSort.go

package main

import "fmt"

func main() {
    values := []int{4, 93, 84, 85, 80, 37, 81, 93, 27,12}
    fmt.Println(values)
    BubbleAsort(values)
    BubbleZsort(values)
}

func BubbleAsort(values []int) {
    for i := 0; i < len(values)-1; i++ {
        for j := i+1; j < len(values); j++ {
            if  values[i]>values[j]{
                values[i],values[j] = values[j],values[i]
            }
        }
    }
    fmt.Println(values)
}

func BubbleZsort(values []int) {
    for i := 0; i < len(values)-1; i++ {
        for j := i+1; j < len(values); j++ {
            if  values[i]<values[j]{
                values[i],values[j] = values[j],values[i]
            }
        }
    }
    fmt.Println(values)
}

 https://github.com/shadow610/leo8/blob/master/BubbleSort.go

posted on 2016-02-22 21:33  zk90  阅读(385)  评论(0编辑  收藏  举报