Speak to the other party's algorithm-quick sort (quick selection)

Sometimes, I think about holding the red algorithm books three or four years ago, and I feel that this is my belief, and I feel very happy, and I have confidence in programming. But you will forget it after reading it, and you can't remember anything.

Now imagine, I have seen it many times, and heard many people say it, and now I forgot it. Quite helpless.

Woke up early one day and listened to the quick sort .

The basic idea:

In a sequence, there are only integers, 8, 2, 5, 7, 4

The first step is to choose a benchmark, such as the first one, 8 (however you choose, you can also optimize, and optimization does not affect the algorithm idea)

The second step is to leave the position of 8, because we have to find the correct order position in the sequence for 8 (because the first position is selected, it can be compared from the end to the front, and it will not be repeated)

The third step is to compare the number in the queue with 8, the small one is placed on the left, and the large one is placed on the right (the operation form is)
3.1-->If you start from the right (repeatedly from the left), the array subscripts To the last digit, 4<8, then 4 is filled in the space, and the sequence is (?) at this time.
Answer: 4257()
3.2-->The array subscript goes to the second to last place, 7<8, don’t move, it’s already in Left. What is the sequence?
The answer is 4257 ()

Continue in order and then you can write it out.
The sequence is 5< 8, 4257()
2 <8, 4257()
4 <8, 4257()
reached the first position,
indicating that the sequence position of 8 has been found, which is the last one. The sequence is
42578

The third step, the operation completes the
fourth step, continue to select the first place, 4 as the benchmark, repeat the third step, the sequence is to
judge whether it has been compared, 8 skips, 7>4, do not move, the sequence is () 2578
5>4 does not move, the sequence is () 2578
2<4, exchange, 2 () 578, has reached the front, indicating that the sequence position of 4 has been found, which is the second
24578

The fifth step, choose to select the next base number in order, 2, the processing before the loop

This is the basic idea.

The extension is quick selection .
Select the Kth number in this sequence (in order, the number in the row)

Scenario: For example, in the sequence 8,2,5,7,4 above, I want to find the third number, from small to large.
Everyone knows the answer, and it should be considered as 5
ideas: after the baseline value is ranked, the left side is smaller than it, even if disorder is not important, you know which position it is.
So to find the k-th number is to find the reference number that should be in the position where there are k-1 numbers in front.

The process of finding is as follows: The
first step is to still follow the quick sort order, and choose a benchmark number (the choice of benchmark number can still be optimized).
We still choose 8. Before learning from: It
means that the sequence position of 8 is found, and it is the last one. The sequence is the second step of
42578
, which compares the number of digits of 8 with the target number of digits.
The comparison target is 3<5, so the right side of the benchmark number is discarded, and the first four digits are quickly sorted. (Look for the third place in this sequence ( please understand it here )) The
remaining sequence is the
third step of 4257 , choose a base number, if you still choose 4 from the beginning,
after comparison, learn from the previous:
explain the sequence of 4 The location is found, it is the second one. The sequence is the fourth step of
2457.
Compare the 4 digits with the target digits and
compare the target, 3> 2, so discard the left side of the benchmark number, and quickly sort from the next two digits.
(Look for the first place in this sequence (3-2=1)) (
please understand here) the fifth step, 57, still choose the beginning, 5, compare from the last place, 7>5, do not move, it is the first A number
indicates that the sequence position of 5 has been found, which is the first. The sequence is
57

                此刻就快速选到了第三位的数字就是5,结束。

                这样我们就描述完了快速排序算法的思想,也顺道延伸了一下快速选择。

                有缘下次再见。

Guess you like

Origin blog.51cto.com/10725691/2639843