Go 是这两年出来的一门语言。比较稚嫩,但还是可以一玩。 复习数据结构之余,顺手写个快速排序--用go实现。

package main

import "fmt"

type ElemType int

func main() {
    data := make([]ElemType, 600000) // ALL ZERO
    var i int = 0
    var dlen int = len(data)
    for i = 0; i < dlen; i++ {
        data[i] = (ElemType)(dlen - i - 1)
    }
    fmt.Println("Start ...", len(data))
    for i = 0; i < 100; i++ {
        fmt.Printf("%d ", data[i])
    }
    fmt.Println()
    QuickSort(data, 0, dlen-1)

    fmt.Println("End ...")
    for i = 0; i < 100; i++ {
        fmt.Printf("%d ", data[i])
    }
    fmt.Println()
}

func QuickSort(A []ElemType, low, high int) {
    if low < high {
        // Partition() is the operation of divide A[low ... high]
        // one to two arrays which can be used as QuickSort Again
        pivotpos := Partition(A, low, high)
        QuickSort(A, low, pivotpos-1)
        QuickSort(A, pivotpos+1, high)
    }
}

func Partition(A []ElemType, low, high int) int {
    var pivot ElemType = A[low]
    var tmp ElemType
    //Method I:
    //for low < high {
    //  for low < high && A[high] >= pivot { high-- ; }
    //  A[low] = A[high];
    //  for low < high && A[low] < pivot { low++; }
    //  A[high] = A[low];
    //}
    //end of MI

    //Method II:
    for (low < high) && (A[high] > pivot) {
        high--
    }
    for (low < high) && (A[low] < pivot) {
        low++
    }
    for low < high {
        // swap A[low] & A[high]
        tmp = A[low]
        A[low] = A[high]
        A[high] = tmp
        low++
        high--
    }
    //end of MII

    A[low] = pivot
    return low
}

执行输出如下:

[yu@argcandargv-com quicksort]$ go build quicksort.go 
[yu@argcandargv-com quicksort]$ ls
quicksort  quicksort.go
[yu@argcandargv-com quicksort]$ time ./quicksort
Start ... 600000
599999 599998 599997 599996 599995 599994 599993 599992 599991 599990 599989 599988 599987 599986 599985 599984 599983 599982 599981 599980 599979 599978 599977 599976 599975 599974 599973 599972 599971 599970 599969 599968 599967 599966 599965 599964 599963 599962 599961 599960 599959 599958 599957 599956 599955 599954 599953 599952 599951 599950 599949 599948 599947 599946 599945 599944 599943 599942 599941 599940 599939 599938 599937 599936 599935 599934 599933 599932 599931 599930 599929 599928 599927 599926 599925 599924 599923 599922 599921 599920 599919 599918 599917 599916 599915 599914 599913 599912 599911 599910 599909 599908 599907 599906 599905 599904 599903 599902 599901 599900 
End ...
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 

real    1m55.564s
user    1m55.215s
sys 0m0.052s
来自的你,很高兴你能看到这儿。若本文对你有所用处,或者内容有什么不足之处,敬请毫不犹豫给个回复。谢谢!