Array
1. Array definition method
method one:
Array name=(value0 value1 value2 …)
Method Two:
Array name=([0]=value [1]=value [2]=value …)
Method three:
List name = "value0 value1 value2 …"
Array name=($list name)
Method four:
Array name [0] = "value"
Array name [1] = "value"
Array name[2]="value"
Note: (20 30 50 40 80 60 70) #value
0 1 2 3 4 5 6 #index or subscript
Second, the data types included in the array and the length of the array
1. Data types included in the array
(1) Numerical type
(2) Character type
Use "" or '' to define
2. Get the length of the array
arr_number=(1 2 3 4 5)
arr_length=${#arr_number[*]}
${#arr_number[@]}
echo $arr_length
Three, array related knowledge
1. Read a subscript assignment
arr_index2=${arr_number[2]}
echo $arr_index2
2. Array traversal
#!/bin/bash
arr_number=(1 2 3 4 5)
for v in ${arr_number [@] }
do
echo $v
done
3. Array slicing
arr=(1 2 3 4 5)
echo ${arr[@]} #output the entire array
echo ${arr[@] :0:2} #Get the value of ${array name[@ or *]: starting position: length}
echo ${arr[@] :2:3}
4. Array replacement
arr=(1 2 3 4 5)
echo Katex the parse error: the Expected 'the EOF', GOT '#' position AT 30: ... # {array name [@ * or] / character find / replace characters}
echo ${arr[@]} #Does not replace the original content of the array
arr=(${arr[@]/4/66}) #To change the original array, it can be realized by re-assignment
echo ${arr[@]}
5. Array delete
arr=(1 2 3 4 5)
unset arr #Delete the array
echo ${arr[*]}
arr=(1 2 3 4 5)
unset arr[2] #Delete the third element
echo ${arr[*]}
6, add elements to the array
method one:
array_name[index]=value
Method Two:
array_name[${#array_name[@]}]=value
Method three:
array_name=("${array_name[@]}" value1 … valueN)
Double quotation marks cannot be omitted, otherwise, when there are elements containing spaces in the array arry_name, the elements will be split into multiple by the spaces
You cannot replace "@" with "*". If you replace it, the behavior of @ without double quotation marks is the same. When double quotation marks are added, all elements in the array array_name will be added to the array as one element.
Method four:
array_name + = (vale1… valueN)
The elements to be added must be surrounded by "()", and multiple elements are separated by spaces.
7. 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.
test1() {
echo "Received parameter list: $@"
newarrary=($1)
echo "The value of the new array: ${array[*]}"
}
array=(3 2 1 4 5)
echo "The value of the original array: ${array[*]}"
test1 $array
(2) To solve this problem, you need to solve the value of the array variable into a single value, and then use these values as function parameters. Inside the function, regroup all the parameters in a new array variable.
test2 () {
newarrary=($(echo $@))
echo "The value of the new array: ${newarrary[*]}"
}
array=(3 2 1 4 5)
echo "The value of the original array: ${array[*]}"
test2 ${array[*]}
Note: $@ stands for all parameters
(3) Return a value from the function (call the element of the new function for function operation)
Example 1: Addition and parameter transfer operation
Example 2: Multiplication and parameter transfer operation
Fourth, the array sorting algorithm
1. Bubble sort
(1) The action similar to the upsurge of bubbles will continuously move the data forward in the array from small to large or from large to small.
(2) Basic ideas:
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, the exchange of two elements Position) so that the smaller elements are like a cheongsam from the bottom to the top.
(3) Algorithm ideas:
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 one, because there is only one array element left in the final loop, no comparison is needed, and the array has been sorted. Up. Second, the inner loop is mainly used to compare the size of each adjacent element in the array. First, determine whether to exchange positions. The number of comparisons and exchanges decreases with the number of sorting rounds.
2. Directly select sort
Compared with bubble sorting, direct selection sorting has fewer exchanges, so the speed will be 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.
3. Reverse the sort
Reorder the contents of the original array in reverse order.
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.