当前位置:网站首页>【golang学习笔记2.1】 golang中的数组中的排序和查找
【golang学习笔记2.1】 golang中的数组中的排序和查找
2022-07-27 06:05:00 【是誰萆微了承諾】
排序
排序是将一组数据,按照指定的规则顺序的排列数据的过程。
1. 冒泡排序
冒泡规则:会进行俩次循环;arr.length-1次循环;第一次【外部循环】每次循环都会出现一个最大数;第二次循环【内部循环】会将比自己大的数进行交换位置。
package main
import (
"fmt"
)
func main() {
//有一个随机数组
var arr [6]int = [...]int{
103, 20, 35, 48, 15, 6}
//循环次数
var num int = len(arr) - 1
//定义接收要交换的值的临时变量
var t int = 0
for i := 0; i < num; i++ {
for j := 0; j < num-i; j++ {
if arr[j] < arr[j+1] {
t = arr[j+1]
arr[j+1] = arr[j]
arr[j] = t
}
}
}
//打印结果:arr: [103 48 35 20 15 6]
fmt.Printf("arr: %v\n", arr)
}
顺序查找
package main
import (
"fmt"
)
func main() {
//有一个随机数组
var arr [6]int = [...]int{
103, 20, 35, 48, 15, 6}
var num int =103
var index int=-1
for i := 0; i < len(arr); i++ {
if num ==arr[i]{
index=arr[i]
break;
}
}
if index !=-1{
fmt.Printf("提示:查找到了 %v",index)
}else{
fmt.Printf("提示:未查找到 %v",index)
}
}
二分查找
package main
import (
"fmt"
)
//从小到大排序
func paixu(arr *[6]int) [6]int {
//循环次数
var num int = len(arr) - 1
//定义接收要交换的值的临时变量
var t int = 0
for i := 0; i < num; i++ {
for j := 0; j < num-i; j++ {
if arr[j] > arr[j+1] {
t = arr[j+1]
arr[j+1] = arr[j]
arr[j] = t
}
}
}
var arr2 = arr
return *arr2
}
//二分查找
//二分查找是对一个有序的数组或者切片进行查找;没有续就不成立了
func BinaryFind(arr *[6]int, leftIndex int, rightIndex int, findValue int) {
//如果左边得下标大于右边得下标;代表已经全部找过了,并且找不到
if leftIndex > rightIndex {
fmt.Printf("未查询到数据")
return
}
//找到中间得下标
middle := (leftIndex + rightIndex) / 2
//如果数组里面得值大于要查找得值,那么就往左边继续查找;反之向右边查找,如果相等那么中间得就是我们要找到得,一次查找就找到了
if (*arr)[middle] > findValue {
BinaryFind(arr, leftIndex, middle-1, findValue)
} else if (*arr)[middle] < findValue {
BinaryFind(arr, middle+1, rightIndex, findValue)
} else {
fmt.Printf("找到了%v", findValue)
return
}
}
func main() {
//有一个随机数组
var arr [6]int = [...]int{
103, 20, 35, 48, 15, 6}
paixu(&arr)
//arr: [6 15 20 35 48 103];因为是传址过去的,所以arr也会改变
fmt.Printf("arr: %v\n", arr)
//二分查找【len(arr-1) 是因为数组得小标是以0开始的】
BinaryFind(&arr, 0, len(arr)-1, 20)
}
二维数组的定义和使用
package main
import (
"fmt"
)
func main() {
//先定义在赋值
var arr [2][3]int
arr[0][2] = 1
arr[1][1] = 2
arr[1][2] = 3
//未赋值的走默认值
//打印结果arr: [[0 0 1] [0 2 3]]
fmt.Printf("arr: %v\n", arr)
//遍历方式一
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr[i]); j++ {
fmt.Print(arr[i][j], "")
}
fmt.Println()
}
//直接初始化
var arr2 [2][3]int = [2][3]int{
{
1, 2}, {
1, 2, 3}}
//打印结果arr2: [[1 2 0] [1 2 3]]
fmt.Printf("arr2: %v\n", arr2)
//遍历方式二
for _, v := range arr2 {
for _,vv:= range v {
fmt.Printf("vv: %v\n", vv)
}
}
}
边栏推荐
- 网易云信亮相 GIAC 全球互联网架构大会,解密新一代音视频架构在元宇宙场景的实践...
- Jmeter:接口自动化测试-BeanShell对数据库数据和返回数据比较
- String类的用法
- Student status management system based on SSM
- MySQL query operation index optimization practice
- Go obtains the processing results of goroutine through channel
- 请问 mysql timestamp(6) 用flink-sql接过来是 null,这点有办法处理不
- Word wrap: break word line feed is compatible with browsers
- 使用反射实现动态修改@Excel的注解属性
- DNA coupled PbSe quantum dots | near infrared lead selenide PbSe quantum dots modified DNA | PbSe DNA QDs
猜你喜欢

李沐动手学深度学习V2-transformer和代码实现

ZnS DNA QDs near infrared zinc sulfide ZnS quantum dots modified deoxyribonucleic acid dna|dna modified ZnS quantum dots

Jmeter:接口自动化测试-BeanShell对数据库数据和返回数据比较

Campus news release management system based on SSM

Hospital reservation management system based on SSM

Qi Yue: thiol modified oligodna | DNA modified cdte/cds core-shell quantum dots | DNA coupled indium arsenide InAs quantum dots InAs DNA QDs

Summary of APP launch in vivo application market

Netease Yunxin appeared at the giac global Internet architecture conference to decrypt the practice of the new generation of audio and video architecture in the meta universe scene

Basic statement of MySQL (1) - add, delete, modify and query

Analysis on the current situation and optimization strategy of customer experience management in banking industry
随机推荐
Golang encapsulates the packages involved in MySQL and the differences between sqlx and Gorm
MySQL quickly compares database table data
Using docker to install and deploy redis on CentOS
Peptide nucleic acid oligomer containing azobenzene monomer (nh2-tnt4, n-pnas) Qiyue biological customization
Analysis on the current situation and optimization strategy of customer experience management in banking industry
Book borrowing management system based on SSM
Interpretation of deepsort source code (II)
DNA modified zinc oxide | DNA modified gold nanoparticles | DNA coupled modified carbon nanomaterials
Es compares the data difference between the two indexes
Working principle analysis of deepsort
Brief introduction of simulation model
Convert Excel to csv/csv UTF-8
Interpretation of deepsort source code (VII)
Consideration on how the covariance of Kalman filter affects the tracking effect of deepsort
How does golang assign values to empty structures
基于SSM学生学籍管理系统
内部类--看这篇就懂啦~
Livox Slam (with lio+ closed loop detection optimization)
Codeforces Round #787 (Div. 3)(7/7)
(转帖)eureka、consul、nacos的对比2