zoukankan      html  css  js  c++  java
  • Go排序

    本文链接:https://blog.csdn.net/u011304970/article/details/71447148

    简介
    Go的sort包提供了排序功能。包括基本类型的排序和自定义数据(通常是结构体数组)的排序。

    基本类型的排序
    sort提供了以下API对基本类型进行排序,查找

    // 排序
    func Ints(a []int)
    func Float64s(a []float64)
    func Strings(a []string)
    
    // 判断是否已经排序
    func IntsAreSorted(a []int) bool
    func Float64sAreSorted(a []float64) bool
    func StringsAreSorted(a []string) bool
    
    // 在已排序的slice中查找,返回索引值,如果找不到,返回len(slice)
    func SearchStrings(a []string, x string) int
    func SearchFloat64s(a []float64, x float64) int
    func SearchInts(a []int, x int) int
    
    package main
    
    import (
        "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 main
    
    import (
        "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 main
    
    import (
        "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
    ", 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) bool
    func Stable(data Interface)
    // 返回data的逆序接口
    func Reverse(data Interface) Interface
    

    例如:

    package main
    
    import (
        "fmt"
        "sort"
    )
    
    type Person struct {
        Name string
        Age  int
    }
    
    type PerColl []Person
    
    var persons PerColl
    
    func (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}]
    }
    
  • 相关阅读:
    【POJ1961】period
    主席树入门
    noip模拟【tea】
    noip模拟【service】
    noip模拟【noname】
    clearfix清除浮动
    九大内置对象
    2017/10/10 jar包错误
    mybatis案例源码详解
    自动类型转换
  • 原文地址:https://www.cnblogs.com/nyist-xsk/p/11365447.html
Copyright © 2011-2022 走看看