Shell array and sorting algorithm

Array

One, define the array

Method 1:
Array name=(value0 value1 value2 …)
Insert picture description here

Method 2:
Array name=([0]=value [1]=value [2]=value …)
Insert picture description here

Method 3:
List name = "value0 value1 value2 …"
Array name = ($list name)
Insert picture description here

Method 4:
Array name [0] = "value"
Array name [1] = "value"
Array name [2] = "value"
Insert picture description here

1. Data types included in the array

● 数值类型
● 字符类型(字符串)
  使用" "''定义

2. Get the length of the array

abc=(10 20 30 40 50 60)
echo $(#abc[*])
echo $(#abc[@])   #获取数组长度

Insert picture description here

3. Read a subscript assignment

abc=(10 20 30 40 50 60)
echo ${
    
    abc[0]}

echo ${
    
    abc[2]}

echo ${
    
    abc[4]

Insert picture description here

Two, array traversal

[root@localhost ~]# vim dome38.sh
#!/bin/bash
arr1=(10 20 30 40 50)
for i in ${
    
    arr1[@]}
do
    echo $i
done

Insert picture description here
Insert picture description here

Three, array slicing

arr2=(1 2 3 4 5 6)
echo ${
    
    arr2[*]}           

echo ${
    
    arr2[@]:0:2}   #获取 ${
    
    数组名[@或*]:起始位置:长度} 的值

echo ${
    
    arr2[@]:1:3}

echo ${
    
    arr2[@]:3:4}

Insert picture description here

Four, array replacement

arr2=(1 2 3 4 5 6)

echo ${
    
    arr2[@]/4/66}   # ${
    
    数组名[@或*]/查找字符/替换字符}
echo ${
    
    arr2[@]}        #并不会替换数组原有内容

arr2=(${
    
    arr2[@]/4/66})  #要实现改变原有数组,可通过重新赋值实现
echo ${
    
    arr2[@]}

Insert picture description here

Five, array deletion

echo ${
    
    arr2[@]}
unset arr2
echo ${
    
    arr2[@]}

Insert picture description here

arr2=(1 2 3 4 5 6 7)
echo ${
    
    arr[*]}

unset arr[5]           #删除数组中选择索引对应的元素
echo ${
    
    arr[*]}

echo ${
    
    arr[6]}

Insert picture description here

Six, add elements to the array

方法一:根据索引单个添加元素
array_name[index]=value

Insert picture description here

方法二:
array_name[${
    
    #array_name[@]}]=value

Insert picture description here

方法三:
array_name=("${#array_name[@]}" value1 value2 ... valueN)
双引号不能省略,否则数组中存在包含空格的元素时会按空格将元素拆分成多个
不能将“@”替换为“*”,如果替换为“*”,不加双引号时与“@”的表现一致,加双引号时,会将数组array_name中的所有元素作为一个元素添加到数组中

Insert picture description here

方法四:
array_name+=(value1 value2 ... valueN)
注:待添加元素必须用“()”包围起来,并且多个元素用空格分隔

Insert picture description here

Seven, pass array parameters to the function

Insert picture description here
Insert picture description here

Eight, sorting algorithm

1. Bubble sort algorithm

Similar to the upsurge of bubbles, the data will continue to move forward 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. If the conditions are met, the element values ​​are exchanged, the smaller element is moved to the front of the array, and the larger element is moved to the back of the array (that is, exchange two The position of each element), so that the smaller element rises from the bottom to the top like a bubble.

  • Algorithm idea: The
    bubbling 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 array to be sorted is reduced by 1, because there is only one array element left in the last loop, and no comparison is required. At the same time, the array The sorting has been completed. The inner loop is mainly used to compare the size of each adjacent element in the array to determine whether to exchange positions. The number of comparisons and exchanges decreases with the number of sorting rounds.

Insert picture description here

#!/bin/bash

arr=(5 1 9 3 6 4)
echo "原数组的排序:${arr[@]}"
length=${
    
    #arr[@]}
#定义比较轮数,比较轮数为数组长度减1,从1开始
for ((i=1; i<$length; i++))
  do
  #确定比较元素的位置,比较相邻两个元素,较大的数往后放,比较次数随着比较轮数而减少  
    for ((a=0; a<$length-i; a++))
    do
   #定义第一个元素的值
    first=${
    
    arr[$a]}
   #定义第二个元素的值
    second=${
    
    arr[$a+1]}
  #如果第一个元素比第二个元素大就互换
      if [ $first -gt $second ]
      then
   #把第一个元素值保存到一个临时变量中
        temp=$first
   #把第二个元素值赋给第一个元素
        arr[$a]=$second
   #把临时变量(也就是第一个元素的原值)赋给第二个元素
        arr[$a+1]=$temp
      fi
    done
done
echo "排序后的数组为:${arr[@]} "

Insert picture description here
Insert picture description here

2. Directly select sort

  • Compared with bubble sorting, direct selection sorting has fewer exchanges, so it is faster.

  • The basic idea:
    Compare the specified sort position with other array elements separately. If the condition is met, the element value will be exchanged. Note that the difference between bubble sorting is not to exchange adjacent elements, but to exchange the elements that meet the condition with the specified sort position (such as Sort from the last element), so that the sorted position gradually expands, and finally the entire array becomes the sorted format.

#!/bin/bash
arr=(60 20 30 50 10 40)
echo "原数组元素顺序为:${arr[@]}"
length=${
    
    #arr[@]}
#定义排序轮数
for ((i=1; i<$length; i++))
do
  #假设索引为0的元素是最大的
  index=0
  #定义和第一个元素比较的索引,来确定最大的元素索引
  for ((a=1; a<=$length-$i ;a++))
  do
  #定义最大的元素的值
    max=${
    
    arr[$index]}
  #定义从索引1开始比较的元素值
    yuansu=${
    
    arr[$a]}
  #判断如果从索引1开始比较的元素大于当前最大元素的值,就记录最大值的索引到index
    if [ $yuansu -gt $max ]
    then
      index=$a
    fi
   done
  #定义每一轮比较的最后一个元素的索引
    last=$[$length-$i]
  #把当前轮次的最后一个元素的值赋给临时变量temp
    temp=${
    
    arr[$last]}
  #把最大的值赋给当前轮次的最后一个元素
    arr[$last]=${
    
    arr[$index]}
  #把temp里的原来最后一个元素的值赋给原最大值所在索引的元素
    arr[$index]=$temp

done

Insert picture description here

Insert picture description here

3. Reverse the sort

  • Reorder the contents of the original array in reverse order

  • The basic idea:
    replace the last element of the array with the first element. The penultimate element is replaced with the second element, and so on, until all the array elements are reversed and replaced

#!/bin/bash
array=(60 20 30 50 10 40)
echo "反转前顺序:${array[*]}"  
length=${
    
    #array[*]}
for ((i=0;i<$length/2;i++))
do
  temp=${
    
    array[$i]}
  array[$i]=${
    
    array[$length-1-$i]}
  array[$length-1-$i]=$temp
done
echo "反转排序后:${array[*]}"

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/IHBOS/article/details/114676862