Array operation method and teach you array sorting (bubbling algorithm, direct sorting algorithm, reverse sorting)
table of Contents
One, the definition method of array
method one:
数组名=(value0 value1 value2 ...)
Method Two:
数`组名=([0]=value [1]=value [2]=value ...)`
Method three:
列表名="value0 value1 value2 ..."
数组名=($列表名)
Method four:
数组名[0]="value"
数组名[1]="value"
数组名[2]="value"
Two, the data type of the array
Numerical type
Character type (string): Use "" or '' to define to prevent spaces in the elements, and the elements are separated by spaces
Three, get the length of the array
[root@localhost ~]#abc=(1 2 3 4 5 6)
[root@localhost ~]#length_abc={#abc[*]}
[root@localhost ~]#echo $length
7
1. Read a subscript assignment
[root@localhost ~]#abc=(1 2 3 4 5
[root@localhost ~]#echo ${abc[3]}
4
2. Array traversal
#!/bin/bash
abc=(1 2 3 4 5)
for v in ${abc[@]}
do
echo $v
done
3. Array slice
abc=(1 2 3 4 5)
echo ${abc[@]} #输出整个数组
echo ${abc[@]:0:2} #获取 ${数组名[@或*]:起始位置:长度} 的值
echo ${abc[*]:1:3}
echo ${abc[@]:2:3}
4. Array replacement
arr=(1 2 3 4 5)
echo ${arr[@]/4/66} #${数组名[@或*]/查找字符/替换字符}
echo ${arr[@]} #并不会替换数组原有内容
arr=(${arr[@]/4/66}) #要实现改变原有数组,可通过重新赋值实现
echo ${arr[@]}
5. Array delete
arr=(1 2 3 4 5)
unset arr #删除数组
echo ${arr[*]}
arr=(1 2 3 4 5)
unset arr[2] #删除第三个元素
echo ${arr[*]}
6, add elements to the array
method one:
abc[index]=value
Method Two:
abc[${#abc[@]}]=value
Method three:
array_name=("${array_name[@]}" value1 ... valueN)
双引号不能省略,否则,当数组array_name中存在包含空格的元素时会按空格将元素拆分成多个
不能将“@”替换为“*”,如果替换为“*”,不加双引号时与“@”的表现一致,加双引号时,会将数组array_name中的所有元素作为一个元素添加到数组中
for i in "${array_name[@]}"; do echo $i; done
Method four:
abc+=(value1 ... valueN)
待添加元素必须用“()”包围起来,并且多个元素用空格分隔
Fourth, pass array parameters to the function
1. If an array variable is used as a function parameter, the function will only take the first value of the array variable.
such as:
vim 11.sh
#!/bin/bash
test1() {
echo "接收到的参数列表:$@"
newarrary=($1)
echo "新数组的值为:${newarrary[*]}"
}
array=(3 2 1 4 5)
echo "原始数组的值为:${array[*]}"
test1 $array
[root@localhost ~]#vim 11.sh
[root@localhost ~]#. 11.sh
原始数组的值为:3 2 1 4 5
接收到的参数列表:3
新数组的值为:3
2. To solve this problem, you need to decompose the values of the array variables into individual values, and then use these values as function parameters. Inside the function, regroup all the parameters into a new array variable
vim 12.sh
#!/bin/bash
test2() {
newarrary=($(echo $@))
echo "新数组的值为:${newarrary[*]}"
}
array=(3 2 1 4 5)
echo "原始数组的值为:${array[*]}"
test2 ${array[*]}
[root@localhost ~]#vim 12.sh
[root@localhost ~]#. 12.sh
原始数组的值为:3 2 1 4 5
新数组的值为:3 2 1 4 5
[root@localhost ~]#
3. Return an array from a function
vim 13.sh
#!/bin/bash
test2() {
newarrary=(`echo $@`)
sum=0
for value in ${newarrary[*]}
do
sum=$[$sum + $value]
done
echo $sum
}
array=(3 2 1 4 5)
echo "原始数组的值为:${array[*]}"
result1=`test2 ${
array[*]}`
echo "新数组的和为:$result1"
[root@localhost ~]#vim 13.sh
[root@localhost ~]#. 13.sh
原始数组的值为:3 2 1 4 5
新数组的和为:15
[root@localhost ~]#
Five, array sorting algorithm
1. Bubble sorting method
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, exchange the element values if the conditions are met, move the smaller element to the front of the array, and move the larger element 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 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. Sorting is complete. 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.
vim 14.sh
#!/bin/bash
abc=(13 4 8 3 9 15 66)
length=${#abc[*]}
for ((i=1;i<$length;i++))
do
for ((j=0;j<$length-$i;j++))
do
first=${abc[$j]}
k=$[$j+1]
second=${abc[$k]}
if [ $first -gt $second ]
then
temp=$first
abc[$j]=$second
abc[$k]=$temp
fi
done
[root@localhost ~]#vim 14.sh
[root@localhost ~]#. 14.sh
3 4 8 9 13 15 66
2. Directly select sort
Compared with bubble sort, direct selection sort has fewer exchanges, so the speed will be faster.
Basic idea:
Compare the specified sort position with other array elements. If the condition is met, the element value will be exchanged. Note that the bubble sort is distinguished here. Instead of exchanging adjacent elements, the element that meets the condition is exchanged 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.
vim 15.sh
#!/bin/bash
abc=(13 4 8 3 9 15 66)
length=${#abc[*]}
for ((i=1;i<$length;i++))
do
index=0
for ((j=1;j<=$length-i;j++))
do
if [ ${abc[$j]} -gt ${abc[$index]} ]
then
index=$j
fi
done
last=$[$length-$i]
temp=${abc[$last]}
abc[$last]=${abc[$index]}
abc[$index]=$temp
done
echo "${abc[*]}"
[root@localhost ~]#vim 15.sh
[root@localhost ~]#. 15.sh
3 4 8 9 13 15 66
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, replace the penultimate element with the second element, and so on, until all the array elements are reversed and replaced.
vim 16.sh
#!/bin/bash
abc=(1 2 3 4 5 6 7)
length=${#abc[*]}
for ((i=0;i<$length/2;i++))
do
temp=${abc[$i]}
abc[$i]=${abc[$length-$i-1]}
abc[$length-$i-1]=$temp
done
echo "${abc[*]}"
[root@localhost ~]#vim 16.sh
[root@localhost ~]#. 16.sh
7 6 5 4 3 2 1