使用golang编写简单的算法

通过编写一些简单的算法学习golang语言。
下面是插入排序算法golang语言的实现:
一般的写法:

package main

import "fmt"

func insertionSort(sortData []interface{}) []interface{} {
    for index := 1; index < len(sortData); index++ {
        key := sortData[index]
        i := index - 1
        for i >= 0 && compare(sortData[i], key) {
            sortData[i+1] = sortData[i]
            i = i - 1
        }
        sortData[i+1] = key
    }
    return sortData
}

func compare(l, r interface{}) bool {
    value, err := compareValue(l, r)
    if err == nil {
        return value
    }
    return false
}

func compareValue(left, right interface{}) (bool, error) {
    switch right := right.(type) {
    default:
        return false, fmt.Errorf("unexpected type %T\n", right)
    case string:
        switch left := left.(type) {
        case string:
            if left > right {
                return true, nil
            }
            return false, nil
        }
    case int:
        switch left := left.(type) {
        case int:
            if left > right {
                return true, nil
            }
            return false, nil
        }
    }

    return false, nil
}

func convertIntToInterface(is []int) []interface{} {
    isc := make([]interface{}, len(is))
    for i, d := range is {
        isc[i] = d
    }
    return isc
}

func convertStringToInterface(is []string) []interface{} {
    isc := make([]interface{}, len(is))
    for i, d := range is {
        isc[i] = d
    }
    return isc
}

func main() {
    is := []int{3, 2, 6, 4, 5}
    ss := []string{"azc", "asd", "bas", "bbc", "zxc"}
    iss := insertionSort(convertIntToInterface(is))
    sss := insertionSort(convertStringToInterface(ss))
    fmt.Println(iss)
    fmt.Println(sss)
}

golang语言sort包里面的写法:

package main

import "fmt"

// Interface copy from sort package
type Interface interface {
    Len() int
    Less(i, j int) bool
    Swap(i, j int)
}

func insertSort(data Interface) {
    for i := 1; i < data.Len(); i++ {
        for j := i - 1; j >= 0 && data.Less(i, j); j-- {
            data.Swap(i, j)
        }
    }
}

// IntSlice attaches the methods of Interface to []int, sorting in increasing order.
type IntSlice []int

func (p IntSlice) Len() int           { return len(p) }
func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p IntSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

// InsertionSort is a convenience method.
func (p IntSlice) InsertionSort() { insertSort(p) }

// StringSlice attaches the methods of Interface to []string, sorting in increasing order.
type StringSlice []string

func (p StringSlice) Len() int           { return len(p) }
func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p StringSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

// InsertionSort is a convenience method.
func (p StringSlice) InsertionSort() { insertSort(p) }

func main() {
    is := []int{3, 2, 6, 4, 5}
    ss := []string{"azc", "asd", "bas", "bbc", "zxc"}
    IntSlice(is).InsertionSort()
    StringSlice(ss).InsertionSort()
    fmt.Println(is)
    fmt.Println(ss)
}

你可能感兴趣的:(使用golang编写简单的算法)