Write in front
Sorting is essentially to arrange a group of numbers in a certain order and repeat them several times, and only put one number in the right position at a time.
Two ways of thinking:
(1) Determine a number first, and then find a suitable location according to the data;
(2) First, determine a location and find a suitable number according to the location;
Bubble sorting algorithm
Determine the position first, choose the front or the back, and suppose to choose the last position, that is, repeatedly put the largest number in the last code implementation.
Selective sorting algorithm
Only the front and rear positions can be selected, and the selected positions are adjusted in sequence forward or backward with each number.
Insertion sorting algorithm
Determine the numbers first, assuming that the previous numbers have been sorted, take them and the next number as the selected numbers, and insert the selected numbers forward to the appropriate position:
Rapid sorting algorithm
Select a number from the head or tail of the array and sort it. For example, the smaller number is on the left and the larger number is on the right. This number is the fulcrum. Every time we compare with the fulcrum and adjust the order, we think that their relative positions have been fixed, so this number is discharged without processing.
Arrange the left and right sides, divide the left and right sides into two parts, choose a number each and do this sort again. Note that only numbers can be selected from both ends of the array, and so on. This can be achieved by a recursive function: