当前位置:网站首页>shell array
shell array
2022-08-10 00:12:00 【Peanut Flavored Peanut Rice】
One.Array
Variable: a memory space for storing a single element
Array: a contiguous memory space for storing multiple elements, equivalent to a collection of multiple variables
2. The role of the array
1. A combination of multiple elements, a collection of variables, and a type of data with the same characteristics are stored in an array.
2. How to distinguish each data in the array and number each element.
3. Classification of Arrays
Ordinary, Associated
Three. Array name and index
The number (subscript) of the index starts from 0 and belongs to the numerical index
The index can support the use of a custom format, not only the numerical format, that is, the associated index, which is supported after the bash 4.0 version.
Bash's arrays support sparse format (indexes are not consecutive)
Four. Declare array
Ordinary arrays can be declared without prior declaration, directly use
declare -a
Associative arrays must be declared first, and then use
declare -A
Normal array


Associative array
1. First declare 2. In assignment

![]()
Five. Array Traversal
Loop output definition value

Six. Array slice
Get the desired result (representing 1 to 3, 0 and others not output)
Seven. Array replacement
/10/80: Change previously defined value to later value![]()
Eight. Bubble sort
Array sorting algorithm
Bubble Sort
Similar to the upward bubble action, the data will move forward continuously in the array from small to large or from large to small.
Basic Ideas
The basic idea of bubble sort is to compare the values of two adjacent elements, and if the conditions are met, exchange the element values,
move the smaller element to the front of the array, and move the larger element to the arrayBehind (that is, swapping the positions of the two elements),
so that the smaller element rises from the bottom to the top like a bubble.
Algorithm ideas
The bubbling algorithm is implemented by a double-layer loop. The outer loop is used to control the number of sorting rounds. Generally, the length of the array to be sorted is reduced by 1, because only one array element is left in the last loop, and no comparison is required., and the array has been sorted.The inner loop is mainly used to compare the size of each adjacent element in the array to determine whether to swap positions. The number of comparisons and swaps decreases with the number of sorting rounds.

![]()
#Define array
read -p "Define array:" shu
a=($shu)
#Assumed to define the maximum number
b=${a[0]}
#loop (values ranging from 0 to the defined maximum number of a, i++: give a jump-out condition to avoid an infinite loop)
for ((i=0;i<${#a[*]};i++))
#The first value is less than the second value, until the end of the loop to find the largest number
do
if [[ $b -lt ${a[$i+1]} ]];then
c=${a[$i+1]}
fi
done
echo "$c"
边栏推荐
猜你喜欢
随机推荐
聊天尬死名场面,你遇到过吗?教你一键获取斗图表情包,晋升聊天达人
One Pass 2074: [21CSPJ Popularization Group] Candy
Janus官方DEMO介绍
leetcode:323. 无向图中连通分量的数目
README_Albumentations
Cesium渐变色3dtiles白模(视频)
【GORM】模型关系-HasMany关系
干涉BGP的选路---社团属性
Common commands and technical function modules of Metasploit
Tencent continues to wield the "big knife" to reduce costs and increase efficiency, and free catering benefits for outsourced employees have been cut
Analyze the Add() method in Fragment management from the source code
【软考 系统架构设计师】案例分析④ 软件架构风格
华为鸿蒙3.0的野望:技术、应用、生态
DXF笔记:文字对齐的研究
R语言ggplot2可视化:使用ggpubr包的ggerrorplot函数可视化误差线(可视化不同水平均值点以及se标准误差)、设置add参数为dotplot添加点阵图
charts.js插件实现的散点图样式
阿里云架构师金云龙:基于云XR平台的视觉计算应用部署
【LaTex】 Font “FandolSong-Regular“ does not contain requested(fontspec)Script “CJK“.如何抑制此种警告?
C. Omkar and Baseball
Flask introductory learning tutorial
![[Microservice~Nacos] Configuration Center of Nacos](/img/c3/9d8fb0fd49a0ebab43ed604f9bd1cc.png)








