参考博客:https://studygolang.com/articles/13173

基本类型排序

package main

import (
"fmt"
"sort"
) func main() {
intList := []int{, , , , , , , , , }
floatList := []float64{4.2, 5.9, 12.3, 10.0, 50.4, 99.9, 31.4, 27.81828, 3.14}
stringList := []string{"a", "c", "b", "d", "f", "i", "z", "x", "w", "y"} sort.Sort(sort.IntSlice(intList))
sort.Sort(sort.Float64Slice(floatList))
sort.Sort(sort.StringSlice(stringList)) fmt.Printf("%v\n%v\n%v\n", intList, floatList, stringList) sort.Sort(sort.Reverse(sort.IntSlice(intList)))
sort.Sort(sort.Reverse(sort.Float64Slice(floatList)))
sort.Sort(sort.Reverse(sort.StringSlice(stringList))) fmt.Printf("%v\n%v\n%v\n", intList, floatList, stringList)
}

结构体排序

package main

import (
"fmt"
"sort"
) type Person struct {
Name string // 姓名
Age int // 年纪
} // 按照 Person.Age 从大到小排序
type PersonSlice []Person func (a PersonSlice) Len() int { // 重写 Len() 方法
return len(a)
}
func (a PersonSlice) Swap(i, j int) { // 重写 Swap() 方法
a[i], a[j] = a[j], a[i]
}
func (a PersonSlice) Less(i, j int) bool { // 重写 Less() 方法, 从小到大排序
return a[i].Age < a[j].Age
} func main() {
people := []Person{
{"zhang san", },
{"li si", },
{"wang wu", },
{"zhao liu", },
} fmt.Println(people) sort.Sort(PersonSlice(people)) // 按照 Age 的升序排序
fmt.Println(people) sort.Sort(sort.Reverse(PersonSlice(people))) // 按照 Age 的降序排序
fmt.Println(people) }

最小堆

heap是常用的实现优先队列的方法。heap包对任意实现了heap接口的类型提供堆操作。堆结构继承自sort.Interface, 而sort.Interface,需要实现三个方法:Len() int / Less(i, j int) bool / Swap(i, j int) 再加上堆接口定义的两个方法:Push(x interface{}) / Pop() interface{}。故只要实现了这五个方法,便定义了一个堆。
package main

import (
"container/heap"
"fmt"
) type IntHeap []int func (h IntHeap) Len() int { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *IntHeap) Push(x interface{}) {
*h = append(*h, x.(int))
} func (h *IntHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-]
*h = old[ : n-]
return x
} func main() {
h := &IntHeap{, , , , , , , }
heap.Init(h)
heap.Push(h, )
fmt.Printf("minimum: %d\n", (*h)[])
for h.Len() > {
fmt.Printf("%d ", heap.Pop(h))
}
}

优先队列

package main

import (
"container/heap"
"fmt"
) type stu struct {
name string
age int
}
type Stu []stu func (t *Stu) Len() int {
return len(*t) //
} func (t *Stu) Less(i, j int) bool {
return (*t)[i].age < (*t)[j].age
} func (t *Stu) Swap(i, j int) {
(*t)[i], (*t)[j] = (*t)[j], (*t)[i]
} func (t *Stu) Push(x interface{}) {
*t = append(*t, x.(stu))
} func (t *Stu) Pop() interface{} {
n := len(*t)
x := (*t)[n-]
*t = (*t)[:n-]
return x
} func main() {
student := &Stu{{"Amy", }, {"Dav", }, {"Spo", }, {"Reb", }}
heap.Init(student)
one := stu{"hund", }
heap.Push(student, one)
for student.Len() > {
fmt.Printf("%v\n", heap.Pop(student))
} }

golang优先队列的更多相关文章

  1. golang实现的简单优先队列

    下面是golang实现的简单优先队列,参考信息可以查看https://golang.org/pkg/container/heap/或者https://golang.google.cn/pkg/cont ...

  2. 数据结构和算法(Golang实现)(24)排序算法-优先队列及堆排序

    优先队列及堆排序 堆排序(Heap Sort)由威尔士-加拿大计算机科学家J. W. J. Williams在1964年发明,它利用了二叉堆(A binary heap)的性质实现了排序,并证明了二叉 ...

  3. 堆 堆排序 优先队列 图文详解(Golang实现)

    引入 在实际应用中,我们经常需要从一组对象中查找最大值或最小值.当然我们可以每次都先排序,然后再进行查找,但是这种做法效率很低.哪么有没有一种特殊的数据结构,可以高效率的实现我们的需求呢,答案就是堆( ...

  4. 面试经典算法:优先队列,最大堆,堆排序,左偏树Golang实现

    堆排序 使用优先队列-最小/最大堆可实现. 优先队列 优先队列是一种能完成以下任务的队列:插入一个数值,取出最小的数值(获取数值,并且删除).优先队列可以用二叉树来实现,我们称这种为二叉堆. 最小堆 ...

  5. golang中container/heap包源码分析

    学习golang难免需要分析源码包中一些实现,下面就来说说container/heap包的源码 heap的实现使用到了小根堆,下面先对堆做个简单说明 1. 堆概念 堆是一种经过排序的完全二叉树,其中任 ...

  6. 数据流中的第k大元素的golang实现

    设计一个找到数据流中第K大元素的类(class).注意是排序后的第K大元素,不是第K个不同的元素. 你的 KthLargest 类需要一个同时接收整数 k 和整数数组nums 的构造器,它包含数据流中 ...

  7. golang模拟动态高优先权优先调度算法

    实验二  动态高优先权优先调度 实验内容 模拟实现动态高优先权优先(若数值越大优先权越高,每运行一个时间单位优先权-n,若数值越小优先权越高,没运行一个时间单位优先权+n),具体如下: 设置进程体:进 ...

  8. 数据结构和算法(Golang实现)(25)排序算法-快速排序

    快速排序 快速排序是一种分治策略的排序算法,是由英国计算机科学家Tony Hoare发明的, 该算法被发布在1961年的Communications of the ACM 国际计算机学会月刊. 注:A ...

  9. 数据结构和算法(Golang实现)(1)简单入门Golang-前言

    数据结构和算法在计算机科学里,有非常重要的地位.此系列文章尝试使用 Golang 编程语言来实现各种数据结构和算法,并且适当进行算法分析. 我们会先简单学习一下Golang,然后进入计算机程序世界的第 ...

随机推荐

  1. C++中两个类中互相包含对方对象的指针问题(转载)

    出处:http://www.cnblogs.com/hanxi/archive/2012/07/25/2608068.html // A.h #include "B.h" clas ...

  2. 将Sublime Text 添加到鼠标右键菜单的教程方法

    安装notepad++软件,在菜单右键自动会添加“edit with notepad++"的选项,那么怎么将Sublime Text 添加到鼠标右键菜单呢?下面是我的操作过程,希望有帮助! ...

  3. ubuntu12 root账户自动登录

    Ubuntu为了系统安全,root帐号的密码是随机的,如果临时需要提升至root权限以执行一些命令,需要使用sudo命令.产线上有几台使用Ubuntu的机器,因为使用者不固定,并且执行程序时需要使 用 ...

  4. 51nod 1428 活动安排问题 (贪心+优先队列)

    来源:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428 首先按照开始时间从小到大排序. 其实只要维护一个结束时间的最 ...

  5. [js] - 关于js的排序sort

    js的排序sort并不能一次排序好 function solution(nums){ return nums.sort(sortNumber); } function sortNumber(a, b) ...

  6. C#下载歌词文件

    前段时间写了一篇c#解析Lrc歌词文件,对lrc文件进行解析,支持多个时间段合并.本文借下载歌词文件来探讨一下同步和异步方法. Lrc文件在网络上随处可见,我们可以通过一些方法获取,最简单的就是别人的 ...

  7. CSAPP学习笔记 第一章 计算机系统漫游

    Ch 1.0 1.计算机系统是由硬件和系统软件组成的 2.本书阐述了计算机组件是如何工作的以及执行组件是如何影响程序正确性和性能的. 3.通过跟踪hello程序的生命周期来开始对系统的学习. #inc ...

  8. Python Sip [RuntimeError: the sip module implements API v11.0 to v11.2 but the PyQt5.QtCore module requires API v11.3]

    不知道原因,尝试卸载.编译安装均失败.只有这样曲线救国 import matplotlib matplotlib.use("WXAgg",warn=True) import mat ...

  9. POJ 2506 Tiling(递推+大整数加法)

    http://poj.org/problem?id=2506 题意: 思路:递推.a[i]=a[i-1]+2*a[i-2]. 计算的时候是大整数加法.错了好久,忘记考虑1了...晕倒. #includ ...

  10. 在centos下解决 “致命错误:curses.h:没有那个文件或目录”

    当在centos下编译带有头文件<curses.h> 的程序时,出现以下错误: “致命错误:curses.h:没有那个文件或目录” ,最后在“https://zhidao.baidu.co ...