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 相关文章推荐
win10下go mod配置方式
Apr 25 Golang
golang gopm get -g -v 无法获取第三方库的解决方案
May 05 Golang
Golang Gob编码(gob包的使用详解)
May 07 Golang
Go 在 MongoDB 中常用查询与修改的操作
May 07 Golang
Golang实现AES对称加密的过程详解
May 20 Golang
Golang生成Excel文档的方法步骤
Jun 09 Golang
go goroutine 怎样进行错误处理
Jul 16 Golang
golang使用map实现去除重复数组
Apr 14 Golang
Golang ort 中的sortInts 方法
Apr 24 Golang
Golang 实现WebSockets
Apr 24 Golang
Go结合Gin导出Mysql数据到Excel表格
Aug 05 Golang
go goth封装第三方认证库示例详解
Aug 14 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
蝙蝠侠:侠影之谜
2020/03/04 欧美动漫
php在线生成ico文件的代码
2007/10/09 PHP
PHP防盗链代码实例
2014/08/27 PHP
php curl上传、下载、https登陆实现代码
2017/07/23 PHP
javascript 混合的构造函数和原型方式,动态原型方式
2009/12/07 Javascript
myeclipse安装jQuery插件的方法
2011/03/29 Javascript
javascript实现rgb颜色转换成16进制格式
2015/07/10 Javascript
JS判断图片是否加载完成方法汇总(最新版)
2016/05/13 Javascript
TinyMCE汉化及本地上传图片功能实例详解
2016/05/31 Javascript
javascript汉字拼音互转的简单实例
2016/10/09 Javascript
用headjs来管理和加载js 提高网站加载速度
2016/11/29 Javascript
解析JavaScript模仿块级作用域
2016/12/29 Javascript
BootStrap3中模态对话框的使用
2017/01/06 Javascript
JavaScript折半查找(二分查找)算法原理与实现方法示例
2018/08/06 Javascript
Node.js Event Loop各阶段讲解
2019/03/08 Javascript
vue视频播放暂停代码
2019/11/08 Javascript
JS实现transform实现扇子效果
2020/01/17 Javascript
js实现html滑动图片拼图验证
2020/06/24 Javascript
Vue实现简单的拖拽效果
2020/08/25 Javascript
详解datagrid使用方法(重要)
2020/11/06 Javascript
[36:33]2018DOTA2亚洲邀请赛 4.3 突围赛 EG vs Newbee 第二场
2018/04/04 DOTA
python爬虫教程之爬取百度贴吧并下载的示例
2014/03/07 Python
详细解析Python中的变量的数据类型
2015/05/13 Python
Python解析Excle文件中的数据方法
2018/10/23 Python
python实现的生成word文档功能示例
2019/08/23 Python
python实现tail -f 功能
2020/01/17 Python
python批量处理多DNS多域名的nslookup解析实现
2020/06/28 Python
用HTML5制作视频拼图的教程
2015/05/13 HTML / CSS
农民工工资承诺书范文
2014/03/31 职场文书
请假条范文大全
2014/04/10 职场文书
植物生产学专业求职信
2014/08/08 职场文书
2014年统计工作总结
2014/11/21 职场文书
机修车间主任岗位职责
2015/04/08 职场文书
2016年度师德标兵先进事迹材料
2016/02/26 职场文书
Python+Matplotlib+LaTeX玩转数学公式
2022/02/24 Python
Redis+AOP+自定义注解实现限流
2022/06/28 Redis