美文网首页
go排序之路(编程基础)

go排序之路(编程基础)

作者: mace3170 | 来源:发表于2018-03-05 20:42 被阅读5次

对python排序有兴趣的可以去我之前的博客查找
TopGun Python排序

冒泡排序

pupu.go(思路 每次遍历都是把最大的放在最右边,然后之后不再对它进行比较,swapped是循环的标志,最后没有前一个比后一个大的情况了,也就是说明排序结束了)


IMG_20180416_181804.jpg
package main

import "fmt"

func main(){
    items := []int{23,43,54,56,76,87,34,1,4,666,55555}
    BubbleSort(items)
    fmt.Println(items)
}

func BubbleSort(items []int){
    var(length = len(items)
        swapped = true)
    for swapped{
            swapped = false
            for index := 0; index < length-1; index++{
                if items[index] > items[index+1]{
                    items[index], items[index+1] = items[index+1], items[index]
                    swapped = true
                }
        }
        length = length - 1
    }
}

运行方式
*该方法会生成一个pupu的二进制文件,直接运行即可

go build pupu.go
./pupu

  • 不会生成二进制文件, 可以直接运行

go run pupu.go


选择排序

第一次循环是针对所有的数据用index进行循环,第二次循环中的jindex就是一个游标而已,由他来衡量出最小的值来,然后跟其与第一次循环的index中的值进行对调

package main

import (
    "fmt"
)

func main() {
    items := []int{12, 32, 43, 45, 23, 23, 54, 23, 76, 23234, 23, 43, 23, 12, 434, 22, 1, 41}
    select_sort(items)
    fmt.Println(items)

}

func select_sort(items []int) {
    var (
        length = len(items)
    )
    for index := 0; index < length-1; index++ {
        mix_index := index
        for jindex := index; jindex < length; jindex++ {
            if items[jindex] < items[mix_index] {
                mix_index = jindex
            }
        }
        items[index], items[mix_index] = items[mix_index], items[index]
    }

}

插入排序

IMG_20180416_191105.jpg

图中有误 比较点是从右往左

package main

import (
    "fmt"
)

func main() {
    items := []int{3, 23, 4, 3, 2, 32, 231, 1, 32, 43, 23, 34, 43, 322, 27, 74, 87, 98}
    insert_sort(items)
    fmt.Println(items)

}

func insert_sort(items []int) {
    length := len(items)
    for index := 1; index < length; index++ {
        jindex := index
        for jindex > 0 {
            if items[jindex] < items[jindex-1] {
                items[jindex], items[jindex-1] = items[jindex-1], items[jindex]
            }
            jindex--
        }
    }

}

希尔排序

(golang中的for也可以当做while用)
希尔排序其实就是插入排序的改善版,你看他的排序核心其实就是比较 『如果前面的元素比后面的元素大 就把这两个元素换一换』,只不过是换的元素是哪几个的问题

package main

import (
    "fmt"
)

func main() {
    items := []int{4, 202, 3, 9, 6, 5, 1, 43, 506, 2, 0, 8, 7, 100, 25, 4, 5, 97, 1000, 27}
    shellshort(items)
    fmt.Println(items)
}

func shellshort(items []int) {
    var length = len(items)
    gap := length / 2
    for gap > 0 {
        for index := gap; index < length; index++ {
            //没想到在这里踩坑 要先判断是否大于等于0 就是要写到&&的前面才行
            for index-gap >= 0 && items[index] < items[index-gap] {
                items[index], items[index-gap] = items[index-gap], items[index]
                index -= gap
            }
        }
        gap = gap / 2
    }
}

相关文章

  • go排序之路(编程基础)

    对python排序有兴趣的可以去我之前的博客查找TopGun Python排序 冒泡排序 pupu.go(思路 每...

  • Golang资料整理

    视频 郝林-Go语言第一课 Go编程基础 Go Web 基础 Go名库讲解 社区 官网要翻墙 Github--Go...

  • Golang 学习笔记一 搭建环境、变量、分支、循环

    学习资料汇总Go by ExampleGo 初学者成长之路系统推荐GO几本书飞雪无情的博客Go Web 编程 电子...

  • go语言学习

    基础 go的学习,感谢Go By Example、go网络编程与go语言标准库随着学习的深入,此文章持续更新......

  • Go编程基础

    Go的优点高效垃圾回收机制类型安全和内存安全(没有隐式转换,只能显示转换)快速编译(未使用包检测)轻松实现高并发支...

  • GO学习笔记(18) - 并发编程(3)- Select与Cha

    本文主要讲解Go并发编程之Select 目录 介绍 基础语法 timeout 综合实例 select 是 Go 中...

  • 从0开始Go语言,用Golang搭建网站

    实践是最好的学习方式 零基础通过开发Web服务学习Go语言 本文适合有一定编程基础,但是没有Go语言基础的同学。 ...

  • 三. Go(Go基础编程)

    一. 面向接口 接口的概念 接口的本质是引入一个新的中间层,调用方可以通过接口与具体实现分离,解除上下游的耦合,上...

  • Go语言入坑

    GO语言基础 认识并安装GO语言开发环境 Go语言简介 Go语言是谷歌2009年发布的第二款开源编程语言 go语言...

  • Go语言探索 - 3(原创)

    Go语言基础系列博客用到的所有示例代码 在上一篇文章中,我们主要学习了Go语言的编程基础。这些基础内容包括注释、分...

网友评论

      本文标题:go排序之路(编程基础)

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