Shell script array. Pass array parameters to function. Return array from function. Bubble sort. Directly select sort

Array definition method

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

The type of data contained in the array

Numerical type
Character type (use "" or '' to define)

Get the length of the array

Insert picture description here
Insert picture description here

Get data list

Insert picture description here

Read a subscript assignment

Insert picture description here

Array traversal

Insert picture description here
Insert picture description here

Array slice

Insert picture description here

Array replacement

Insert picture description here
Insert picture description here
Insert picture description here

Array delete

Insert picture description here
Insert picture description here

Array append element

Method 1:
Insert picture description here
Method 2:
Insert picture description here

Method 3:
Insert picture description here
Double quotation marks cannot be omitted, otherwise, when there are elements in the array array_name that contain spaces, the elements will be split into multiple by spaces.
You cannot replace "@" with " ". If you replace it with" ", do not add double quotation marks. Is consistent with the performance of "@". When double quotation marks are added, all elements in the array array_name will be added to the array as one element

Method 4: The
Insert picture description here
elements to be added must be surrounded by "()", and multiple elements must be separated by spaces

Pass array parameters to the function

If an array variable is used as a function parameter, the function will only take the first value of the array variable.
Insert picture description here

Insert picture description here
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, all the parameters are recombined into a new array variable.
Insert picture description here
Insert picture description here

Return an array from a function

Insert picture description here
Insert picture description here
Insert picture description here

Bubble Sort

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, exchange the element values, 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
bubble 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 one, because there is only one array element left in the last loop, and there is no need for comparison. The 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.

Insert picture description here
Insert picture description here
Insert picture description here

Direct selection 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 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.

Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/MQ107/article/details/114677018