Basic application of array and bubbling algorithm
1. Array
1. 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"
-
Note: The array does not have to be ordered or can be disordered.
The subscript of the index must start from 0. The
string should be defined with ""double colon" or "single colon". If it is not defined, there will be spaces.
2. Data types included in the array
- Data types included in the array
- Numerical type
- Character type
- Use "" or '' to define
3. Get the length of the array
4. Read index assignment
5. Array traversal
6. Array slicing
7. Array replacement
8. Array delete
9. 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.
2. Bubble sort
- Similar to the upsurge of bubbles, the data will continue to move forward in the array from small to large.
- 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, the exchange of 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.