Golang ort 中的sortInts 方法


Posted in Golang onApril 24, 2022

前言:

排序算法一直是很经常使用的功能。Go 语言标准库为我们提供了方便快捷的 ​​sort​​ 包 ,这个包实现了四种基本排序算法:插入排序、归并排序、堆排序和快速排序。

一、从有序数据中查找值

我们知道,常见查找算法有顺序查找和二分查找。而二分查找就是基于有序数据的查找方法。而 Go 语言中的 ​​sort​​ 包就提供了以下几种查找的方法:

  • SearchInts(slice ,val)
  • SearchFloats(slice, val)
  • SearchStrings(slice, val)
  • Searh(count, testFunc)

二、SearchInts

​SearchInts()​​ 函数是 sort 包的内置函数,用于在排序的整数切片中搜索给定元素 ​​x​​,并返回 ​​Search()​​ 指定的索引。

它接受两个参数(​​a []int, x int​​):

  • a 是 int 类型的排序切片,
  • x 是要搜索的 int 类型元素,并返回​​Search()​​ 指定的索引

注意:如果 ​​x​​ 不存在,可能是 ​​len(a)​​,​​SearchInts()​​ 结果是插入元素 ​​x​​ 的索引。切片必须按升序排序。

语法结构如下:

func SearchInts(a []int, x int) int

返回值: ​​SearchInts()​​ 函数的返回类型是 int,它返回 Search 指定的索引。

三、举例

例子一:

package main

import (
"fmt"
"sort"
)

func main() {

ints := []int{2025, 2019, 2012, 2002, 2022}

sortInts := make([]int, len(ints))

copy(sortInts, ints)

sort.Ints(sortInts)

fmt.Println("Ints: ", ints)
fmt.Println("Ints Sorted: ", sortInts)

indexOf2022 := sort.SearchInts(sortInts, 2022)
fmt.Println("Index of 2022: ", indexOf2022)
}

运行该代码:

$ go run main.go
Ints: [2025 2019 2012 2002 2022]
Ints Sorted: [2002 2012 2019 2022 2025]
Index of 2022: 3

例子二:

package main

import (
"fmt"
"sort"
)

func main() {
a := []int{10, 20, 25, 27, 30}

x := 25
i := sort.SearchInts(a, x)
fmt.Printf("Element %d found at index %d in %v\n", x, i, a)

x = 5
i = sort.SearchInts(a, x)
fmt.Printf("Element %d not found, it can inserted at index %d in %v\n", x, i, a)

x = 40
i = sort.SearchInts(a, x)
fmt.Printf("Element %d not found, it can inserted at index %d in %v\n", x, i, a)
}

运行结果:

Element 25 found at index 2 in [10 20 25 27 30]
Element 5 not found, it can inserted at index 0 in [10 20 25 27 30]
Element 40 not found, it can inserted at index 5 in [10 20 25 27 30]

到此这篇关于Go 语言sort 中的sortInts 方法的文章就介绍到这了!


Tags in this post...

Golang 相关文章推荐
Golang 正则匹配效率详解
Apr 25 Golang
golang 实现两个结构体复制字段
Apr 28 Golang
解决golang post文件时Content-Type出现的问题
May 02 Golang
go语言中GOPATH GOROOT的作用和设置方式
May 05 Golang
解决goland 导入项目后import里的包报红问题
May 06 Golang
go开发alertmanger实现钉钉报警
Jul 16 Golang
使用GO语言实现Mysql数据库CURD的简单示例
Aug 07 Golang
简单聊聊Golang中defer预计算参数
Mar 25 Golang
Golang 对es的操作实例
Apr 20 Golang
Golang 入门 之url 包
May 04 Golang
在ubuntu下安装go开发环境的全过程
Aug 05 Golang
Go结合Gin导出Mysql数据到Excel表格
Aug 05 Golang
Golang 切片(Slice)实现增删改查
Apr 22 #Golang
Golang 结构体数据集合
Apr 22 #Golang
Golang map映射的用法
Apr 22 #Golang
Golang bufio详细讲解
Apr 21 #Golang
Go获取两个时区的时间差
Apr 20 #Golang
Golang jwt身份认证
实现GO语言对数组切片去重
Apr 20 #Golang
You might like
PHP 引用文件技巧
2010/03/02 PHP
Warning: session_destroy() : Trying to destroy uninitialized sessionq错误
2011/06/16 PHP
ThinkPHP 模板substr的截取字符串函数详解
2017/01/09 PHP
PHP实现链式操作的三种方法详解
2017/11/16 PHP
跟随鼠标旋转的文字
2006/11/30 Javascript
setTimeout和setInterval的浏览器兼容性分析
2007/02/27 Javascript
js 数组的for循环到底应该怎么写?
2010/05/31 Javascript
js confirm()方法的使用方法实例
2013/07/13 Javascript
jquery()函数的三种语法介绍
2013/10/09 Javascript
自己动手实现jQuery Callbacks完整功能代码详解
2013/11/25 Javascript
单击和双击事件的冲突处理示例代码
2014/04/03 Javascript
判断及设置浏览器全屏模式
2014/04/20 Javascript
Javascript+CSS实现影像卷帘效果思路及代码
2014/10/20 Javascript
jquery实现简单的二级导航下拉菜单效果
2015/09/07 Javascript
JS实现的颜色实时渐变效果完整实例
2016/03/25 Javascript
JavaScript中数组的22种方法必学(推荐)
2016/07/20 Javascript
AngularJS入门教程之REST和定制服务详解
2016/08/19 Javascript
JavaScript实现鼠标点击导航栏变色特效
2017/02/08 Javascript
微信小程序自动客服功能
2017/11/02 Javascript
node.js通过axios实现网络请求的方法
2018/03/05 Javascript
微信小程序实现电子签名功能
2020/07/29 Javascript
浅谈vue.watch的触发条件是什么
2020/11/07 Javascript
[09:37]2018DOTA2国际邀请赛寻真——不懈追梦的Team Serenity
2018/08/13 DOTA
[04:20]DOTA2-DPC中国联赛 正赛 VG vs LBZS 选手采访 1月19日
2021/03/11 DOTA
python日志记录模块实例及改进
2017/02/12 Python
解决Tensorboard 不显示计算图graph的问题
2020/02/15 Python
学校消防安全制度
2014/01/30 职场文书
怎样写好自我评价呢?
2014/02/16 职场文书
2014政务公开实施方案
2014/02/19 职场文书
计算机毕业生求职信
2014/06/10 职场文书
文艺晚会策划方案
2014/06/11 职场文书
民族学专业求职信
2014/07/28 职场文书
2014年综治维稳工作总结
2014/11/17 职场文书
孔繁森观后感
2015/06/10 职场文书
入党函调证明材料
2015/06/19 职场文书
Redis数据结构之链表与字典的使用
2021/05/11 Redis