当前位置:网站首页>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"
边栏推荐
- 守护进程
- p5.js实现的炫酷星体旋转动画
- APS系统能消除造成生产和运输延迟的瓶颈
- R语言检验时间序列的平稳性:使用tseries包的adf.test函数实现增强的Dickey-Fuller(ADF)检验、检验时序数据是否具有均值回归特性(平稳性)、不具有均值回归特性的案例
- R语言ggstatsplot包grouped_ggscatterstats函数可视化分组散点图、并添加假设检验结果(包含样本数、统计量、效应大小及其置信区间、显著性、组间两两比较、贝叶斯假设)
- 第 1 章 一大波数正在靠近——排序
- This article lets you quickly understand implicit type conversion [integral promotion]!
- openGauss数据库基本操作(超详细)
- Common commands and technical function modules of Metasploit
- UNI-APP_监听页面滚动h5监听页面滚动
猜你喜欢
开发者必备:一文快速熟记【数据库系统】和【软件开发模型】常用知识点
Arcgis工具箱无法使用,显示“XML包含错误“的解决方法
leetcode:332. 重新安排行程
小程序+自定义插件的关键性
[Microservice~Nacos] Configuration Center of Nacos
JSON 基本使用
力扣 1413. 逐步求和得到正数的最小值
This article lets you quickly understand implicit type conversion [integral promotion]!
Blender程序化建模简明教程【PCG】
第十七期八股文巴拉巴拉说(数据库篇)
随机推荐
2022年中国第三方证券APP创新专题分析
Domestic mobile phone manufacturers once fought for it, but now it is the first to collapse...
毕昇编译器优化:Lazy Code Motion
浅析量股票化交易的发展现状
华为鸿蒙3.0的野望:技术、应用、生态
JSON 基本使用
Leetcode.25 K个一组翻转链表(模拟/递归)
C. Omkar and Baseball
Core Data浅谈系列之五 : 在UITableView中展示
Interviewer: How to deal with Redis big key?
leetcode:286.墙和门
Flask入门学习教程
R语言将列表数据转化为向量数据(使用unlist函数将列表数据转化为向量数据)
OSS文件上传
【软考 系统架构设计师】案例分析⑤ 质量属性和架构评估
聊天尬死名场面,你遇到过吗?教你一键获取斗图表情包,晋升聊天达人
R语言ggplot2可视化:使用ggpubr包的ggscatter函数可视化散点图、使用scale_x_continuous函数的breaks参数指定X轴的断点的个数(设置参数n)
R语言使用mean函数计算样本(观测)数据中指定变量的相对频数:计算时间序列数据中大于前一个观测值的观测值所占的比例总体的比例
Chapter 15 HMM模型
R语言修改dataframe数据列的名称:使用dplyr包的rename函数修改列名、使用colnmaes函数修改列名、在数据筛选的时候重命名列名