Article Directory
-
- Array definition method
- The type of data contained in the array
- Get the length of the array
- Get data list
- Read a subscript assignment
- Array traversal
- Array slice
- Array replacement
- Array delete
- Array append element
- Pass array parameters to the function
- Return an array from a function
- Bubble Sort
- Direct selection sort
Array definition method
Method 1:
Array name=(value0 value1 value2 …)
Method 2:
Array name=([0]=value [1]=value [2]=value …)
Method 3:
List name=“value0 value1 value2…”
Array name =($List name)
Method 4:
Array name [0] = "value"
Array name [1] = "value"
Array name [2] = "value"
The type of data contained in the array
Numerical type
Character type (use "" or '' to define)
Get the length of the array
Get data list
Read a subscript assignment
Array traversal
Array slice
Array replacement
Array delete
Array append element
Method 1:
Method 2:
Method 3:
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
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.
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.
Return an array from a function
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.
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.