Recursion depth Fast Sorting Algorithm

Original Address

https://blog.csdn.net/qq_33758761/article/details/76782610

Title: n recording linear table for rapid sorting algorithm to reduce the depth of recursion, the following statements is correct (A)
A, after each partition, the first shorter processing portion
B, after each partition, the first handle more long portion
C, as the algorithm for each partition independent of the processing sequence
D, none of the above three
Explanation:
in the quick sort, it is necessary to use a recursive process the left and right sub-segment sub-segments, respectively, can be understood as the depth of the recursive system stack depth stored, the first processing segment reprocessing short length segment, the time complexity can be reduced.
If you press long recursive priority, then the short recursion will remain in the stack until the process is completed long segments. Short priority, then, no longer recursive calls, as a whole it is stored on the stack, so the stack recursion recursive data retained will be less.

See a lot of people say do not understand, I'll give you an example in the other answer.
There are so sequences: 123456789; each assuming the length of the short sequence is divided into 1
i.e. the first division
short sequences: a
long sequence: 23456789
If short sequences of priority 1
and stack stored in only 23,456,789, a depth of 1
and 23456789 stack, dividing
the short sequence: 2
long sequence: 3456789
similar to the processing sequence of short
stack stored 3,456,789, a depth of 1
and so on down, processing the entire sequence, the maximum depth of the stack 1 are
short sequences if each partitioned 2 it is the length of
the short sequence: 12
long sequence: 3456789
priority 12 short sequences
stack depth is stored 3456789 1
12 can be divided into a sequence of the same length 1 and 2
to the left is processed
stack stored in the stack 2 at this time and there are 3456789 2 depth of 2
and then 2 stack processing
then 3,456,789 stack
is divided into 34 short sequence
length sequence 56789
handle short sequence of 34
save 56,789 stack
and so on down, processing the entire sequence, the maximum depth of the stack are 2
means that the maximum stack depth depends on the sequence length of the short split up into (provided to process short sequences)
So first we handle long sequence
of short sequences: 1
long sequence: 23,456,789
If you give priority to long sequences 23,456,789 short stack sequence, sequence length and divided into 2 3,456,789
2 stack, 3,456,789 divided
. . .
Drawing 8, 9 processing
at this time has a depth of 8 stack 12345678
short sequence: 12
long sequence: 3,456,789
12 3,456,789 stack is divided into 34 and 56 789
34 56 789 divided into a stack 789 and 56
56 789 divided into a stack 78 and 9
9 and the stack 78 is divided into 7 8
7 8 stack processing
time there stack 12345697 depth of 5
apparent depth to the processing sequence of length greater than the depth of the stack to the first stack processing the short sequence, the answer is not a look that is known.

Guess you like

Origin blog.csdn.net/alexhu2010q/article/details/81019403