Go排序
来源:互联网 发布:网络加速app 编辑:程序博客网 时间:2024/05/14 10:44
简介
Go的sort包提供了排序功能。包括基本类型的排序和自定义数据(通常是结构体数组)的排序。
基本类型的排序
sort提供了以下API对基本类型进行排序,查找
// 排序func Ints(a []int)func Float64s(a []float64)func Strings(a []string)// 判断是否已经排序func IntsAreSorted(a []int) boolfunc Float64sAreSorted(a []float64) boolfunc StringsAreSorted(a []string) bool// 在已排序的slice中查找,返回索引值,如果找不到,返回len(slice)func SearchStrings(a []string, x string) intfunc SearchFloat64s(a []float64, x float64) intfunc SearchInts(a []int, x int) int
例子:
package mainimport ( "fmt" "sort")func main() { is := []int{3, 5, 2} fmt.Println(sort.IntsAreSorted(is)) // false sort.Ints(is) fmt.Println(is) // [2 3 5] fmt.Println(sort.IntsAreSorted(is)) // true fmt.Println(sort.SearchInts(is, 5)) // 2 fmt.Println(sort.SearchInts(is, 8)) // 3}
自定义类型的排序
自定义类型的排序分为两种,一种是通过传递函数值,一种是通过实现接口
传递函数值
// 对slice排序func Slice(slice interface{}, less func(i, j int) bool)func SliceStable(slice interface{}, less func(i, j int) bool) // 稳定排序// 判断是否已经排序func SliceIsSorted(slice interface{}, less func(i, j int) bool) bool// 二分法查找,这个函数很有意思,它查找并返回使函数f返回true和返回false的临界点(见下例)func Search(n int, f func(int) bool) int
Slice()和SliceIsSorted()用法
package mainimport ( "fmt" "sort")type Person struct { Name string Age int}func main() { persons := []Person{ Person{"Gopher", 11}, Person{"Monkey", 12}, Person{"Cynhard", 5}, } // 按照名字排序 less := func(i, j int) bool { return persons[i].Name < persons[j].Name } fmt.Println(sort.SliceIsSorted(persons, less)) // false sort.Slice(persons, less) fmt.Println(persons) // [{Cynhard 5} {Gopher 11} {Monkey 12}] fmt.Println(sort.SliceIsSorted(persons, less)) // true}
Search()用法
package mainimport ( "fmt" "sort")func main() { ints := []int{22, 34, 21, 32, 54, 64, 49, 43} sort.Ints(ints) fmt.Println(ints) // [21 22 32 34 43 49 54 64] // ints[0]~ints[1]都小于32,ints[2]~ints[7]都大于等于32 // 因此临界点索引为2,found==2 found := sort.Search(len(ints), func(i int) bool { return ints[i] >= 32 }) fmt.Println(found) // 2 if found < len(ints) && ints[found] == 32 { fmt.Printf("32 found at index %d\n", found) } else { // 没有找到32,但是返回了32应该插入的位置 fmt.Println("32 not found") } // 由于找不到一个临界点,使序列左边为32,右边不为32 // 所以返回len(ints),found==8 found = sort.Search(len(ints), func(i int) bool { return ints[i] == 32 }) fmt.Println(found) // 8}
实现接口
sort提供了一个接口:sort.Interface,所有满足这个接口的自定义类型都可以进行排序。API如下:
type Interface interface { Len() int Less(i, j int) bool Swap(i, j int)}func Sort(data Interface)func IsSorted(data Interface) boolfunc Stable(data Interface)// 返回data的逆序接口func Reverse(data Interface) Interface
例如:
package mainimport ( "fmt" "sort")type Person struct { Name string Age int}type PerColl []Personvar persons PerCollfunc (PerColl) Len() int { return len(persons)}func (PerColl) Less(i, j int) bool { return persons[i].Name < persons[j].Name}func (PerColl) Swap(i, j int) { persons[i], persons[j] = persons[j], persons[i]}func main() { persons = []Person{ Person{"Cynhard", 5}, Person{"Gopher", 11}, Person{"Monkey", 12}, } sort.Sort(persons) fmt.Println(persons) // [{Cynhard 5} {Gopher 11} {Monkey 12}] fmt.Println(sort.IsSorted(persons)) // true sort.Sort(sort.Reverse(persons)) fmt.Println(persons) // [{Monkey 12} {Gopher 11} {Cynhard 5}]}
1 0
- Go排序
- 【go】插入排序
- 睡眠排序--go实现
- 快速排序----Go语言版
- Go语言冒泡排序
- <11> go sort排序
- go排序示例
- Go实现位图排序
- Go实现并发排序
- Go实现堆排序
- Go编写计数排序
- 随笔----go语言版快速排序
- go语言实现快速排序
- GO实现 快速排序算法
- 排序算法三个(by go)
- Go实现快速排序算法
- Go语言实现拓扑排序
- go使现快速排序
- Windows右键新建md文件
- 运算符
- Unity加载AssetBundle后shader丢失问题
- [LeetCode]371.Sum of Two Integers两数相加
- openlayers3中如何叠加png图片
- Go排序
- WebStorm设置服务器-局域网内其他设备访问(移动端手机访问测试等)
- KMP字符串模式匹配算法解析
- 170508
- 数据库范式:1NF、2NF、3NF、BCNF
- poj3131 立体八数码,双向广搜
- Codeforces 486D Valid Sets【树型Dp】
- SOA演进到微服务
- VS2013 T4模板的问题自己解决