美文网首页每天写1000字每天写500字
使用golang编写简单的算法

使用golang编写简单的算法

作者: 昙花未现 | 来源:发表于2016-10-17 22:32 被阅读97次

通过编写一些简单的算法学习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编写简单的算法

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

  • 开始编写Golang代码

    开始编写Golang代码 介绍 本文主要讲述如何写一个简单的Go包和如何使用golang的工具,如何获取、编译和安...

  • go语言vim怎么设置

    golang vim 插件设置 多数情况使用vim编写golang代码,需要有语法高亮显示以及自动format。为...

  • 记一次golang gc优化过程

    我们使用golang编写的线上服务,通常会设置一个golang runtime指标监控,包括goroutine n...

  • Rboot 文档--简介

    Rboot 助手机器人 介绍 Rboot是一个使用golang编写的,简单、高效的聊天机器人框架,易于扩展,它可以...

  • 「golang」绘制爱心文本

    这两天利用最近所学,编写了一个 Golang 绘制爱心文本程序。 用法如下: 我们简单看下使用的效果: 在除夕之夜...

  • Tank技术【3-1】GoLang学习资源

    一、序言:用GoLang编写服务端程序 Go语言属于类C语言,由Google维护,排名火箭上升。GoLang使用方...

  • golang 模板使用示例

    生产环境keepalived.conf 文件使用golang模板渲染示例 模板文件编写示例:

  • 一篇文章搞懂柏林噪声算法,附代码讲解

    本文以一种通俗简单的方式介绍Ken Perlin的改进版柏林噪声算法,算法代码采用c#编写,开源免费使用。如果你只...

  • 第二章:排序基础

    选择排序算法(selectionSort) 算法思想: 算法图示: 使用模板(泛型)编写算法:随机生成算法测试用例...

网友评论

    本文标题:使用golang编写简单的算法

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