Shell programming array and bubble algorithm sorting detailed explanation

Four expressions of an array

Method 1:
Array name=(value0 value1 value2 …)
Method 2:
Array name=([0]=value [1]=value [2]=value)
Method 3:
List name="value0 value1 value2 …"
Array name= ($list name)
Method 4:
Array name[0]="value"
Array name[1]="value"
Array name[2]="value"
……

Addition, deletion, modification and check operation of two array

Get the length of the array
arr_length=${#arr_number[*]}
array slice

[root@localhost ~]# a=(1 2 3 4 5)
[root@localhost ~]# echo ${a[*]}  		//输出整个数组
1 2 3 4 5
[root@localhost ~]# echo ${a[@]:0:2}  #${数组名[@或*]:起始位置:长度]
1 2 
[root@localhost ~]# echo ${a[@]:2:3}
3 4 5
注意:将数组切片之后,返回的是字符串,以空格作为分隔符

Array replacement

arr=(1 2 3 4 5)
echo ${arr[@]/4/66}  //${数组名[@或*]/查找字符/替换字符}
1 2 3 66 5
echo ${arr[@]}       //并不会替换数组原有内容
1 2 3 4 5
a=(${arr[@]/4/66})		//要实现改变原有数组,可通过重新赋值实现
echo ${arr[*]}
1 2 3 66 5

Delete array

[root@localhost ~]# arr=(1 2 3 4 5)
[root@localhost ~]# unset arr		//删除整个数组

[root@localhost ~]# unset arr[2]		//删除第三个元素
[root@localhost ~]# echo ${arr[*]}
1 2 4 5

Add element to array

方法1 array_name[index]=value
方法2 array_name[${#array_name[@]}]=value
方法3 array_name=("${array_name[@]}" value1 ... valueN)
方法4 array_name+=(value1 value 2 ... valueN)

Insert picture description here

note:
The double quotation marks in "${array_name[@]}" cannot be omitted, otherwise, when an
element containing a space appears in the array array_name, the element will be split into multiple by the space

You cannot replace @ with *, otherwise all elements in the array array_name will be added to the array as one element when you add double quotes
Insert picture description here

Three groups of parameters

3.1 The main program passes array parameters to the function

The following problems may occur when calling arrays in functions

#!/bin/bash
test1() {
    
    
echo "函数接收到的列表为:$@"
newarray=$1
echo ${newarray[*]}
}
array=(3 2 1 4 5)
echo "原始的数组值为${array[*]}"
test1 $array

The result of the operation is shown in the figure below.
Insert picture description here
At this time, you need to decompose the values ​​of the array variables into individual values, and then use these values ​​as function parameters. Inside the function, all the parameters are recombined into a new array variable.
The demo code is as follows:

#!/bin/bash
test2() {
    
    
newarray=($(echo $@))		//$@会把接收到的所有的位置参数都表示出来,并且分割成单个元素。此处也可以写成newarray=($@)
echo "新数组的值为:${newarray[*]}"
}
array=(3 2 1 4 5)
echo "原始的数组值为${array[*]}"
test2 ${array[*]}		//此处{
    
    }必加,表示把这个数组看作一个整体

The results are as follows
Insert picture description here

3.2 Return array from function to main program

test3() {
    
    
newarray=(`echo $@`)
for((i=0;i<$#;i++))
{
    
    
newarray[$i]=$[${newarray[$i]}*2]
}
echo ${newarray[*]}
}

array=(3 2 1 4 5)
result=($(test3 ${
     
     array[*]}))
echo "新数组的值为:${result[*]}"

Four bubble algorithm sorting

Bubble sorting is
similar to the action of rising bubbles, moving the data in the array from small to large or from large to small.

Basic idea:
The basic idea of ​​bubble sorting is to compare the values ​​of two adjacent elements, exchange the element values ​​if the conditions are met, and move the smaller element to the back of the array (that is, exchange the positions of the two elements), so that it is smaller The elements rise from the bottom to the top like bubbles.

Algorithm idea The
bubble algorithm is implemented by a double-layer loop, where the outer loop is used to control the number of sorting rounds, generally the length of the sorted array is reduced by one, because there is only one array element left in the last loop, no comparison is needed, and the array has been completed Sorted out. The inner loop is mainly used to compare the size of each adjacent element in the array to determine whether to swap positions.

Code

#!/bin/bash
paixu() {
    
    
array=($1)
for ((i=1;i<${#array[*]};i++)) ##比较轮数为数组长度减1,从1开始
do
        for ((j=0;j<${#array[*]}-i;j++))  ##比较相邻两个元素,较大的数往后放,比较次数随比较比较轮数而减少
        do
        if [ ${array[$j]} -gt ${array[$[$j+1]]} ]  ##如果第一个元素比第二个元素大就互换
        then
        temp=${array[$j]}  ##把第一个元素值保存在临时变量中
        array[$j]=${array[$[$j+1]]}  ##把第二个元素值保存到第一个元素中
        array[$[$j+1]]=$temp  ##把临时变量(也就是第一个元素原值),保存到第二个元素中
        fi
        done
done
echo ${array[*]}
}
#主体代码
f=$(cat $1)
paixu $f

The results are as follows
Insert picture description here

Guess you like

Origin blog.csdn.net/cenjeal/article/details/108128512