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 相关文章推荐
go原生库的中bytes.Buffer用法
Apr 25 Golang
解决golang在import自己的包报错的问题
Apr 29 Golang
解决go在函数退出后子协程的退出问题
Apr 30 Golang
golang gopm get -g -v 无法获取第三方库的解决方案
May 05 Golang
go mod 安装依赖 unkown revision问题的解决方案
May 06 Golang
golang 实现时间戳和时间的转化
May 07 Golang
Golang全局变量加锁的问题解决
May 08 Golang
go xorm框架的使用
May 22 Golang
Go语言并发编程 sync.Once
Oct 16 Golang
golang生成vcf通讯录格式文件详情
Mar 25 Golang
Golang并发工具Singleflight
May 06 Golang
Go语言怎么使用变长参数函数
Jul 15 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
用文本文件制作留言板提示(下)
2006/10/09 PHP
PHP nl2br函数 将换行字符转成 <br>
2009/08/21 PHP
linux下为php添加curl扩展的方法
2011/07/29 PHP
php命名空间学习详解
2014/02/27 PHP
php计划任务之ignore_user_abort函数实现方法
2015/01/08 PHP
win10下 php安装seaslog扩展的详细步骤
2020/12/04 PHP
js数字输入框(包括最大值最小值限制和四舍五入)
2009/11/24 Javascript
无缝滚动js代码通俗易懂(自写)
2013/06/19 Javascript
JS 排序输出实现table行号自增前端动态生成的tr
2014/08/13 Javascript
JavaScript代码复用模式详解
2014/11/07 Javascript
JavaScript计算某一天是星期几的方法
2015/08/05 Javascript
网页挂马方式整理及详细介绍
2016/11/03 Javascript
Bootstrap页面缩小变形的快速解决办法
2017/02/03 Javascript
BootStrap Validator 根据条件在JS中添加或移除校验操作
2017/10/12 Javascript
如何编写一个完整的Angular4 FormText 组件
2017/11/18 Javascript
详解react-redux插件入门
2018/04/19 Javascript
Jquery属性的获取/设置及样式添加/删除操作技巧分析
2019/12/23 jQuery
JavaScript前端实现压缩图片功能
2020/03/06 Javascript
python从ftp下载数据保存实例
2013/11/20 Python
如何利用Fabric自动化你的任务
2016/10/20 Python
Python虚拟环境项目实例
2017/11/20 Python
python 模拟银行转账功能过程详解
2019/08/06 Python
python 动态调用函数实例解析
2019/10/21 Python
如何在django中运行scrapy框架
2020/04/22 Python
Pandas将列表(List)转换为数据框(Dataframe)
2020/04/24 Python
如何在sublime编辑器中安装python
2020/05/20 Python
MxNet预训练模型到Pytorch模型的转换方式
2020/05/25 Python
Python爬虫代理池搭建的方法步骤
2020/09/28 Python
HTML5中的拖放实现详解
2017/08/23 HTML / CSS
.net笔试题
2014/03/03 面试题
完美主义个人的自我评价
2014/02/17 职场文书
单位授权委托书范文
2014/08/02 职场文书
邻里守望志愿服务活动方案
2014/08/15 职场文书
工作证明格式范文
2015/06/15 职场文书
2016大学先进团支部事迹材料
2016/03/01 职场文书
考教师资格证不要错过的4个最佳时机
2019/07/17 职场文书