Golang 如何对一段Search进行排序

Golang 如何对一段Search进行排序

Golang语言提供了内置支持实现基本常量和运行时反射的功能,以操作sort包。Golang有独立运行函数的能力。通过这个函数,我们可以通过导入“sort”包轻松地对整数和字符串进行排序。这些函数被定义在sort包中,因此需要在程序中导入sort包。sort函数可以搜索任何重要的Golang排序函数列表,如下所示:

语法:

func Search(n int, f func(int) bool) int
Go

返回值: 该函数返回范围[0,n)中f(i)为true的最小索引i,假设在范围[0,n)上,f(i)==true意味着f(i+1)==true。

此函数仅用于对Search进行排序。以下示例说明了Golang中使用上述方法的用法:

示例1:

package main
import (
"fmt"
"sort"
)
func main() {
// 未排序的整数切片
intSlice := []int{10, 3, 6, 10, 15, 21, 38, 26, 25, 45}
a := 15
pos := sort.SearchInts(intSlice, a)
fmt.Printf("在 %v 中找到%d ,索引为%d\n", intSlice, a, pos)
// 未排序的字符串切片
strSlice := []string{"Pink", "Orange","Green", "Black","Purple", "Blue", "Red"}
b := "Green"
pos = sort.SearchStrings(strSlice, b)
fmt.Printf("在 %v 中找到%s ,索引为%d\n",strSlice,b, pos)
// 未排序的浮点数切片
fltSlice := []float64{612.15, 114.510,211.144, 396.242, 485.143}
c := 211.144
pos = sort.SearchFloat64s(fltSlice, c)
fmt.Printf("在 %v 中找到%f ,索引为%d\n", fltSlice, c, pos)
// 按降序排列的已排序切片
d := []int{45, 38, 26, 25, 21, 15, 10, 10, 6, 3}
e := 38
i := sort.Search(len(d), func(i int) bool { return d[i] <= e })
if i < len(d) && d[i] == e {
fmt.Printf("在 %v 中找到%d ,索引为%d\n", d, e, i)
} else {
fmt.Printf("%d 在 %v 中未找到\n", e, d)
}
}
Go

输出:

[10 3 6 10 15 21 38 26 25 45] 中找到15 ,索引为4[Pink Orange Green Black Purple Blue Red] 中找到Green ,索引为6[612.15 114.51 211.144 396.242 485.143] 中找到211.144000 ,索引为2[45 38 26 25 21 15 10 10 6 3] 中找到38 ,索引为1
Go

示例2:

给定示例说明了按升序排序的列表在不同形式中的搜索。

package main
  
import (
    "fmt"
    "sort"
)
  
func main() {
  
    // 未排序的
    s := []int{19, 42, 24, 63, -20, 59}
    sort.Sort(sort.IntSlice(s))
  
    // 已排序的
    fmt.Println(s)
    fmt.Println("Slice的长度为:", sort.IntSlice.Len(s))
  
    fmt.Println("Slice中40的位置为:",
        sort.IntSlice(s).Search(40))
  
    fmt.Println("-10在Slice中的位置为:",
        sort.IntSlice(s).Search(6))
    fmt.Println("-----------------------------------")
  
    t := []string{"Pink", "Orange", "Green",
        "Black", "Purple", "Blue", "Red"}
  
    sort.Sort(sort.StringSlice(t))
  
    fmt.Println(t)
    fmt.Println("Slice的长度为:", sort.StringSlice.Len(t))
  
    fmt.Println("Slice中Black的位置为:",
        sort.StringSlice(t).Search("Black"))
  
    fmt.Println("Slice中Orange的位置为:",
        sort.StringSlice(t).Search("Orange"))
  
    fmt.Println("-----------------------------------")
  
    // 未排序的
    u := []float64{602.15, 194.10, 611.144, 396.42, 655.433}
    sort.Sort(sort.Float64Slice(u))
  
    // 已排序的
    fmt.Println(u)
    fmt.Println("Slice的长度为:", sort.Float64Slice.Len(u))
  
    fmt.Println("Slice中611.144的位置为:",
        sort.Float64Slice(u).Search(611.144))
  
    fmt.Println("Slice中194.10的位置为:",
        sort.Float64Slice(u).Search(194.10))
} 
Go

输出:

[-20 19 24 42 59 63]
Slice的长度为: 6
Slice中40的位置为: 3
-10在Slice中的位置为: 1
-----------------------------------
[Black Blue Green Orange Pink Purple Red]
Slice的长度为: 7
Slice中Black的位置为: 0
Slice中Orange的位置为: 3
-----------------------------------
[194.1 396.42 602.15 611.144 655.433]
Slice的长度为: 5
Slice中611.144的位置为: 3
Slice中194.10的位置为: 0
Go

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册