Sorting Algorithm Notes - Selection Sort

Important Sorts: Insertion Sort, Heap Sort, Merge Sort, Quick Sort.
insert image description here

Picking bubbles and inserting
, returning to the pile, statistics,
Enfangen, Lao En, one three,
to Enga K, En multiplying K

selection sort

insert image description here

Filter the array over and over, then find the smallest number in the array and put it in the front.
In the first pass, filter the entire array to find the smallest number 1, and then swap the positions of 1 and the first number 5.
The second time, because the first smallest number has been found, that is, the remaining part except the first number 1, find the smallest number, and repeat the process. .
insert image description here
Specific example: such as 10 people lined up in a queue from low to high.
The first is to find the shortest person from the 10 people and put it in the first one, and then start from the second person to find the second shortest person. . . . . until all arrangements are complete.

—————————————————— Specific steps:
first record the subscript of the minimum value, assuming that the initial minimum subscript is 0, that is, nums[0], use the minimum value to subscript The number at the marked position is compared with the next number, 5 is compared with 2, 2 is smaller, at this time, the minimum subscript will be changed, and the subscript will be changed to 1, and then continue to compare, 2 and 4 are compared, or 2 is small, and all the minimum subscripts at this time remain unchanged. Then compare the value 2 of the minimum subscript with the value 1 of the next element , and find that 1 is smaller. At this time, the subscript of the minimum value is transformed into the index 3 corresponding to element 1. At this time, the minimum value is 1, and then the minimum value and the lower value are changed. One element ratio , 1 and 3 ratio, found that 1 is still the smallest, and the subscript remains unchanged. At this point, the array has been traversed once, and the index value 3 of the minimum value has been found.
insert image description here
At this time, the index position of the minimum value is exchanged with the first number in the array.
insert image description here
Then treat the array with the first value removed as a brand new array and repeat the previous steps.
insert image description here

Write algorithm flow

Write whatever you can, write as much as you can;
write a few steps and run. Run a lot, make sure the middle is no problem, and continue.

Find an example and try it out.
insert image description here
Record the position of the minimum value, assuming that the first position is 0.
insert image description here
Then the elements of the minimum value and the first position are swapped.
insert image description here
Then remove the first number, see the latter number as a new array, and repeat the previous steps. . . .

That is, changing the code structure and constructing a loop.
insert image description here
Fine, encapsulate the swap into a method:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_45942265/article/details/118436011