当前位置:网站首页>[golang learning notes 2.1] sorting and searching in arrays in golang
[golang learning notes 2.1] sorting and searching in arrays in golang
2022-07-27 07:38:00 【Who made the promise】
Sort
Sorting is to put a set of data , The process of arranging data in order according to specified rules .
1. Bubble sort
Bubbling rule : There will be two cycles ;arr.length-1 Secondary cycle ; for the first time 【 External circulation 】 A maximum number will appear in each cycle ; The second cycle 【 Internal circulation 】 Will exchange positions for numbers larger than yourself .
package main
import (
"fmt"
)
func main() {
// There is a random array
var arr [6]int = [...]int{
103, 20, 35, 48, 15, 6}
// cycles
var num int = len(arr) - 1
// Define temporary variables that receive values to be exchanged
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
}
}
}
// Print the results :arr: [103 48 35 20 15 6]
fmt.Printf("arr: %v\n", arr)
}
In order to find
package main
import (
"fmt"
)
func main() {
// There is a random array
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(" Tips : We found it %v",index)
}else{
fmt.Printf(" Tips : Not found %v",index)
}
}
Two points search
package main
import (
"fmt"
)
// Sort from small to large
func paixu(arr *[6]int) [6]int {
// cycles
var num int = len(arr) - 1
// Define temporary variables that receive values to be exchanged
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
}
// Two points search
// Binary search is to find an ordered array or slice ; If there is no continuation, it will not be established
func BinaryFind(arr *[6]int, leftIndex int, rightIndex int, findValue int) {
// If the left subscript is greater than the right subscript ; The representative has looked for all , And I can't find
if leftIndex > rightIndex {
fmt.Printf(" No data found ")
return
}
// Find the middle subscript
middle := (leftIndex + rightIndex) / 2
// If the value in the array is greater than the value to be found , Then continue to look to the left ; On the contrary, look to the right , If equal, then the middle is what we want to find , You can find it in one search
if (*arr)[middle] > findValue {
BinaryFind(arr, leftIndex, middle-1, findValue)
} else if (*arr)[middle] < findValue {
BinaryFind(arr, middle+1, rightIndex, findValue)
} else {
fmt.Printf(" eureka %v", findValue)
return
}
}
func main() {
// There is a random array
var arr [6]int = [...]int{
103, 20, 35, 48, 15, 6}
paixu(&arr)
//arr: [6 15 20 35 48 103]; Because it was addressed , therefore arr It will change too.
fmt.Printf("arr: %v\n", arr)
// Two points search 【len(arr-1) It's because the subscript of the array is 0 At the beginning 】
BinaryFind(&arr, 0, len(arr)-1, 20)
}
Definition and use of two-dimensional array
package main
import (
"fmt"
)
func main() {
// First define in assignment
var arr [2][3]int
arr[0][2] = 1
arr[1][1] = 2
arr[1][2] = 3
// Unassigned values go to default
// Print the results arr: [[0 0 1] [0 2 3]]
fmt.Printf("arr: %v\n", arr)
// Traversal mode 1
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr[i]); j++ {
fmt.Print(arr[i][j], "")
}
fmt.Println()
}
// Direct initialization
var arr2 [2][3]int = [2][3]int{
{
1, 2}, {
1, 2, 3}}
// Print the results arr2: [[1 2 0] [1 2 3]]
fmt.Printf("arr2: %v\n", arr2)
// Traversal mode 2
for _, v := range arr2 {
for _,vv:= range v {
fmt.Printf("vv: %v\n", vv)
}
}
}
边栏推荐
- Panabit SNMP configuration
- Oracle cleans up the Database disk space of tables with referenced partitions
- Pg_ relation_ Size question
- Bash: create a function that returns a Boolean value
- glGetUniformLocation,glUniform4f
- UUID与secrets模块
- Array method and loop in JS
- Understanding and learning of node flow and processing flow in io
- Functools module
- Help send a recruitment, base all over the country. If you are interested, you can come and have a look
猜你喜欢

Prior Attention Enhanced Convolutional Neural Network Based Automatic Segmentation of Organs at Risk

(2022 Hangdian multi school III) 1009.package delivery (greedy)

记录一个自己挖的坑~

Understanding and learning of node flow and processing flow in io

Temperature and humidity measurement and display device based on Arduino

单臂路由(讲解+实验)

Graylog 日志服务器单节点部署

TCP/IP协议分析(TCP/IP三次握手&四次挥手+OSI&TCP/IP模型)

(2022杭电多校三)1009.Package Delivery(贪心)

STM32_ Find the cause of entering hardfault_ Handler's function
随机推荐
A priority SQL problem
Use reflection to dynamically modify annotation attributes of @excel
Flink de duplication (I) summary of common de duplication schemes in Flink and Flink SQL
Analysis of memory structure of C program code
when的多条件查询
UUID and secrets module
杂谈:高考
What are the main threads of Youxuan database?
ADC噪声全面分析 -01- ADC噪声的类型以及ADC特性
JS regular expression implementation adds a comma to every three digits
Help send a recruitment, base all over the country. If you are interested, you can come and have a look
mysql备份策略
View the dmesg log before server restart
Flink principle (I) TTL management and fault tolerance mechanism of state
(2022杭电多校三)1009.Package Delivery(贪心)
Cadence (XI) silk screen printing adjustment and subsequent matters
Use the PIP command to switch between different mirror sources
Array method and loop in JS
Analysis of query results using both left join on and where in MySQL
C language programming | program compilation and preprocessing