Golang学习 - sort 包
------------------------------------------------------------// 满足 Interface 接口的类型可以被本包的函数进行排序。 type Interface interface {// Len 方法返回集合中的元素个数Len() int// Less 方法报告索引 i 的元素是否比索引 j 的元素小Less(i, j int) bool// Swap 方法交换索引 i 和 j 的两个元素的位置Swap(i, j int) }// 对 data 进行排序(不保证相等元素的相对顺序不变) // data 默认为升序,执行 Reverse 后为降序。 func Sort(data Interface)// 对 data 进行排序(保证相等元素的相对顺序不变) // data 默认为升序,执行 Reverse 后为降序。 func Stable(data Interface)// 将 data 的排序动作更改为降序,Reverse 并不改变元素顺序,只改变排序行为。 // 更改操作不可逆,更改后的对象不可以再次 Reverse。 func Reverse(data Interface) Interface// 判断 data 是否已经排序 // 未执行 Reverse 的必须为升序,执行 Reverse 的必须为降序 func IsSorted(data Interface) bool------------------------------// 示例 func main() {i := []int{3, 7, 1, 3, 6, 9, 4, 1, 8, 5, 2, 0}a := sort.IntSlice(i)fmt.Println(sort.IsSorted(a)) // falsesort.Sort(a)fmt.Println(a) // [0 1 1 2 3 3 4 5 6 7 8 9]fmt.Println(sort.IsSorted(a), "\n") // trueb := sort.IntSlice{3}fmt.Println(sort.IsSorted(b), "\n") // true// 更改排序行为c := sort.Reverse(a)fmt.Println(sort.IsSorted(c)) // falsefmt.Println(c) // &{[0 1 1 2 3 3 4 5 6 7 8 9]}sort.Sort(c)fmt.Println(c) // &{[9 8 7 6 5 4 3 3 2 1 1 0]}fmt.Println(sort.IsSorted(c), "\n") // true// 再次更改排序行为d := sort.Reverse(c)fmt.Println(sort.IsSorted(d)) // falsesort.Sort(d)fmt.Println(d) // &{0xc42000a3b0}fmt.Println(sort.IsSorted(d)) // truefmt.Println(d) // &{0xc42000a3b0} }------------------------------// 对 a 进行升序排列 func Ints(a []int)// 判断 a 是否为升序排列 func IntsAreSorted(a []int) bool// 搜索 a 中值为 x 的索引,如果找不到,则返回最接近且大于 x 的值的索引, // 可能是 len(a)。 func SearchInts(a []int, x int) int------------------------------// 示例 func main() {a := []int{3, 9, 1, 6, 4, 2, 8, 2, 4, 5, 3, 0}sort.Ints(a)fmt.Println(a) // [0 1 2 2 3 3 4 4 5 6 8 9]fmt.Println(sort.IntsAreSorted(a)) // truei := sort.SearchInts(a, 7)fmt.Println(a[i]) // 8 }------------------------------// 功能同上,类型不同 func Float64s(a []float64) func Float64sAreSorted(a []float64) bool func SearchFloat64s(a []float64, x float64) int// 功能同上,类型不同 func Strings(a []string) func StringsAreSorted(a []string) bool func SearchStrings(a []string, x string) int------------------------------// 实现了 sort.Interface 接口的 []int 类型 type IntSlice []int func (p IntSlice) Len() int // 接口方法 func (p IntSlice) Less(i, j int) bool // 接口方法 func (p IntSlice) Swap(i, j int) // 接口方法// 对 p 进行升序排列 func (p IntSlice) Sort()// 搜索 p 中值为 x 的索引,如果找不到,则返回最接近且大于 x 的值的索引, // 可能是 len(a)。 func (p IntSlice) Search(x int) int------------------------------// 示例 func main() {a := sort.IntSlice{3, 7, 1, 3, 6, 9, 4, 1, 8, 5, 2, 0}a.Sort()fmt.Println(a) // [0 1 1 2 3 3 4 5 6 7 8 9]fmt.Println(sort.IsSorted(a)) // truei := a.Search(6)fmt.Println(i, a[i]) // 8 6 }------------------------------// 功能同上,类型不同 type Float64Slice []float64 func (p Float64Slice) Len() int func (p Float64Slice) Less(i, j int) bool func (p Float64Slice) Swap(i, j int) func (p Float64Slice) Sort() func (p Float64Slice) Search(x float64) int// 功能同上,类型不同 type StringSlice []string func (p StringSlice) Len() int func (p StringSlice) Less(i, j int) bool func (p StringSlice) Swap(i, j int) func (p StringSlice) Sort() func (p StringSlice) Search(x string) int------------------------------// Search 采用二分法搜索,在小于 n 的索引中查找最小的满足 f(索引) 的值。返 // 回找到的索引,如果没有符合要求的索引,则返回 n。 func Search(n int, f func(int) bool) int------------------------------// 示例 func main() {a := sort.StringSlice{"hello", "world", "golang", "sort", "nice"}a.Sort() // 二分法必须先排序// 获取首字母大于 n 的元素中最小的i := sort.Search(len(a), func(i int) bool {return len(a[i]) > 0 && a[i][0] > 'n'})// 显示找到的元素fmt.Println(a[i]) // sort }------------------------------------------------------------
转载于:https://www.cnblogs.com/golove/p/5924653.html
Golang学习 - sort 包相关推荐
- golang使用sort包排序
1. sort包简介 sort包实现了四种基本排序算法:插入排序.归并排序.堆排序和快速排序. 但是,这四种排序方法不是公开的,它们只在sort包内部被使用.所以,在对数据集合排序时,不必考虑应当选择 ...
- Golang学习 - bufio 包
------------------------------------------------------------// bufio 包实现了带缓存的 I/O 操作---------------- ...
- Golang学习 - sync 包
------------------------------------------------------------临时对象池Pool 用于存储临时对象,它将使用完毕的对象存入对象池中,在需要的时 ...
- Golang学习 - unicode 包
------------------------------------------------------------const (MaxRune = '\U0010FFFF' // Unicode ...
- Golang学习(10)——bufio包
Golang学习 - bufio 包 ------------------------------------------------------------ // bufio 包实现了带缓存的 I/ ...
- Golang学习(12)——regex包
Golang学习 - regexp 包 ------------------------ // 函数 // 判断在 b(s.r)中能否找到 pattern 所匹配的字符串 func Match(pat ...
- Golang sort 包使用
1.sort包简介 Golang 中的标准库 sort 包为切片及用户定义的集合的排序操作提供了原语.sort包提供了对内置类型切片的排序支持,如 []int切片.[]float64切片和[]stri ...
- Golang sort包Search函数源码分析
首先放今天的力扣打卡题. 在第一次做的过程中,我忽略了"升序排列"这个条件,没有使用二分. 由于是最近才刚开始学golang,所以很有兴趣的在这道题里使用了go的大杀器--goro ...
- golang中的包管理工具——govendor和godep简单学习
为什么用vendor目录 依赖问题 我们知道,一个工程稍大一点,通常会依赖各种各样的包.而Go使用统一的GOPATH管理依赖包,且每个包仅保留一个版本.而不同的依赖包由各自的版本工具独立管理,所以当所 ...
- golang 排序list_Go语言使用sort包对任意类型元素的集合进行排序的方法
本文实例讲述了Go语言使用sort包对任意类型元素的集合进行排序的方法.分享给大家供大家参考.具体如下: 使用sort包的函数进行排序时,集合需要实现sort.Inteface接口,该接口中有三个方法 ...
最新文章
- Dataset之MapillaryVistas:MapillaryVistas数据集的简介、下载、使用方法之详细攻略
- OAuth2.0 知多少
- 下午带着几个同学打了两节课的牌~
- iBooker AI+财务提升星球 2020.4 热门讨论
- 2018年最新整理ios APP审核被拒的常见原因
- POJ3980 取模运算【水题】
- 【SSH网上商城项目实战28】使用Ajax技术局部更新商品数量和总价
- react 移动端视频、音频、pdf预览
- MATLAB中前馈+反馈系统搭建-基于matlab控制系统工具箱
- coco2017数据集百度网盘链接
- 导航动态避让算法RVO的优化ORCA(Optimal Reciprocal Collision Avoidance)
- 自适应辛普森(Simpson)积分及二重积分
- three相机在模型上_threejs学习心得(场景的搭建+运动模型导入)
- 虎牙不想做一家游戏直播公司
- ps的切片用来转换html,Photoshop切片导出HTML+CSS
- 杨承润:世界首席创业家导师杨承润,创业16年,杨承润和他的《慧眼经营思维》
- win32 007
- 测试喇叭SPL软件,SPL 立体声监听控制器 MTC 2381 评测
- Flexnbsp;强淫效果
- 视频教程-JavaScript拼图游戏视频教程-JavaScript