Basic application of array and bubbling algorithm

1. Array

1. 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

  • 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

Insert picture description here

4. Read index assignment

Insert picture description here

5. Array traversal

Insert picture description here

Insert picture description here

6. Array slicing

Insert picture description here

7. Array replacement

Insert picture description here

8. Array delete

Insert picture description here

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.

Insert picture description here

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.
  • Insert picture description here
    Insert picture description here
    Insert picture description here

Guess you like

Origin blog.csdn.net/LI_MINGXUAN/article/details/111941178