Shell-array and sort
1. Array definition method
1. Method One
Array name=(value0 value1 value2 …)
array1=(10 20 30 40 50)
2. Method two
Array name=([0]=value [1]=value [2]=value …)
array2=([0]=10 [1]=20 [2]=30 [3]=40 [4]=50)
3. Method three
List name = "value0 value1 value2 …"
Array name = ($list name)
abc="10 20 30 40 50"
array3=($abc)
4. Method Four
Array name [0] = "value"
Array name [1] = "value"
Array name [2] = "value"
array4[0]=10
array4[1]=20
array4[2]=30
array4[3]=40
array4[4]=50
Two, the data types included in the array
1. Numerical type
Character type (string): Use "" or '' to define to prevent spaces in the elements, and the elements are separated by spaces
2. Get the length of the array
echo ${#Array name[*]}
array1=(10 20 30 40 50)
echo ${#array1[*]}
3. Read an index assignment
echo ${数组名[*]}——读取整个数组数值
echo ${数组名[x]}——读取索引x+1的数值
echo ${array1[*]}
echo ${array1[1]}
echo ${array1[3]}
4. Array traversal
#!/bin/bash
arr=(50 40 30 20 10)
for i in ${arr[@]}
do
echo $i
done
5. Array slicing
Get the value of ${array name[@ or *]: start position: length} and
output the entire array, where * is the same as @
arry1=(10 20 30 40 50)
echo ${arry1[*]}
echo ${arry1[*]:0:2}
echo ${arry1[*]:1:4}
echo ${arry1[*]:2:2}
6. Array replacement
①Replace a single value
array1[2]=88
②Multiple replacement (temporary replacement)
echo ${array1[*]/0/66}
7. Array delete
①The entire array deletes the
unset array name
unset arry
②Delete the specified position
unset array name [index number]
unset array3[2]
8. Add elements to the array
①Method 1
Add individually
Array name [X]=Y
array3[2]=30
② Method two (the middle value element must not have omissions)
without any deletion, the maximum value of the index is the element length minus one.
Array name [$#{Array name[*]}]=X
array3[${#array3[*]}]=88
③Method Three
Get all the elements of the source array directly plus the newly added elements, and re-assign the array, refresh the definition index.
Double quotes cannot be omitted, otherwise, if there are elements in the array that contain spaces, the elements will be split into multiples by spaces. one
can not be "@" replaced with "*", consistent with the "@" performance if replaced by asterisks, without the single quotes, double quotes when all the elements in the array will be array_name as an element added to the array Medium
array name=("${array name[@]}" numeric element x numeric element y numeric element z)
array3=("${array3[@]}" 99 100)
④Method 4
Array name += (value x value y value z)
array3+=(200 300 400)
Three, pass array parameters
#!/bin/bash
fun () {
echo "函数收到的值为:$@"
newarr=$@
echo "新数组的值为:${newarr[@]}"
}
arr=(60 20 30 40 50)
echo "arr数组的值为:${arr[@]}"
fun ${arr[@]}
Fourth, return an array from a function
1. Example 1
#!/bin/bash
test (){
newarr=($@)
sum=0
for i in ${newarr[@]}
do
sum=$[$sum+$i]
done
echo $sum
}
arr=(10 20 30 40 50)
echo "原数组arr的值为:${arr[@]}"
res1=$(test ${arr[@]})
echo "新数组的所有数值和为:$res1"
2. Example 2
#!/bin/bash
test () {
newarr=($@)
# echo "函数接收的数组1为:${newarr[@]}"
for ((i=0;i<${#newarr[@]};i++))
do
newarr[$i]=$[${newarr[$i]} * 2]
done
echo ${newarr[@]}
}
arr=(10 20 30 40 50)
echo "原数组的值为:${arr[@]}"
res1=$(test ${arr[@]})
echo "新数组的值为:${res1[@]}"
~
Five, sorting algorithm
1. Bubble sort
①Concept
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.
②Positive sequence
#!/bin/bash
read -p "输入需要排序的数组:" x
array=($x)
echo "旧数组为:${array[@]}"
#获取数组长度
length=${#array[@]}
for ((i=1;i<$length;i++))
do
#确定数值位置,大的往后放,并且比较次数随着轮数增加而减少
for ((a=0;a<$length-$i;a++))
do
#定义第一个和第二个比较的数值
first=${array[$a]}
second=${array[$a+1]}
if [ $first -gt $second ];then
#把第一个数值赋予给一个临时变量temp
temp=${array[$a]}
#把第二个数值赋予给第一个数值
array[$a]=${array[$a+1]}
#再把第一个数值的值(临时变量temp)赋予给第二个数值
array[$a+1]=$temp
fi
done
done
echo "排序后的数组顺序为:${array[@]}"
③Reverse order
#!/bin/bash
paixu () {
array=($@)
echo "旧数组为:${array[@]}"
#获取数组长度
length=${#array[@]}
for ((i=1;i<$length;i++))
do
#确定数值位置,大的往后放,并且比较次数随着轮数增加而减少
for ((a=0;a<$length-$i;a++))
do
#定义第一个和第二个比较的数值
first=${array[$a]}
second=${array[$a+1]}
if [ $first -lt $second ];then
#把第一个数值赋予给一个临时变量temp
temp=${array[$a]}
#把第二个数值赋予给第一个数值
array[$a]=${array[$a+1]}
#再把第一个数值的值(临时变量temp)赋予给第二个数值
array[$a+1]=$temp
fi
done
done
echo "排序后的数组顺序为:${array[@]}"
}
list=$@
paixu $list
2. Directly select sort
Direct selection sorting
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=(20 60 50 30 10 40)
echo "原数组的值为:${arr[@]}"
length=${#arr[@]}
#定义排序轮数
for ((a=1;a<$length;a++))
do
#假设索引为0的元素最大
index=0
#定义和第一个数值比较索引,确定最大元素索引
for ((b=1;b<=$length-$a;b++))
do
#定义最大的数值
max=${arr[$index]}
#定义从索引1开始比较数值
value=${arr[$b]}
#判断如果从索引1开始比较的数值大于当前最大的数值,就记录最大的索引到ind
if [ $value -gt $max ];then
index=$b
fi
done
#定义每一轮比较的最后一个数值索引
last=$[$length-$a]
#把当前轮次的最后一个数值赋给临时变量temp
temp=${arr[$last]}
#把最大的值赋给当前轮次的最后一个数值
arr[$last]=${arr[$index]}
#把temp里的原来最后一个数值赋给原最大值所在索引的数值
arr[$index]=$temp
done
echo "排序后数组为:${arr[@]}"
3. Reverse the sort
#!/bin/bash
arr=(10 20 30 40 50)
length=${#arr[@]}
for ((i=0;i<$length/2;i++))
do
temp=${arr[$i]}
arr[$i]=${arr[$length-$i-1]}
arr[$length-$i-1]=$temp
done
echo ${arr[@]}