Classic sorting algorithm-shell sort
Shell sort is based onInsert sortIs also divided into two parts,
Part 1: Introduction to Hill sorting
The second part is how to select keywords. Selecting keywords is the key to Hill sorting.
Introduction to the First Hill sorting
Prepare the array to be sorted [6 2 4 1 5 9]
First, you need to select keywords. For example, the key is 3 and 1 (the first step is divided into three groups, and the second step is divided into one group). The arrays to be arranged are divided into the following three virtual groups:
[6 1] A group
[2 5] Group 2
[4 9] Three Groups
Look at it carefully. Instead of grouping two numbers, we divide the numbers in multiples of 3 (divided into three groups) into one group,
It is to take one number every three and one number every three. In this way, the numbers are put in a group,
Consider them as a group, but they are not actually grouped, just as a group, so the above "group" does not actually exist, just to describe the group relationship
For the above three groupsInsert sortTo the bottom.
[1 6] [2 5] [4 9]
Specific process:
[6 1] 6 and 1 switch to [1 6]
[2 5] 2 and 5 remain unchanged or [2 5]
[4 9] 4 and 9 remain unchanged or [4 9]
Demonstration of the first sort status:
Array to be sorted :[62 415 9]
Array after sorting :[12 465 9]
The second keyword is 1, that is, every other one is taken to form a new array. In fact, there is only one group, and it is retrieved every other time.
The array to be sorted is: [1 2 4 6 5 9]
Directly performInsert sort
RememberInsert sortWhy not? Review
[1 2 4] does not need to be moved, and the process is omitted. At the time of 5, 5 is taken out, and the position suitable for it is inserted in the ordered array of the front edge, that is, after 4, 6 is inserted.
There is no need to change the back, so the sorting is complete.
Output results in sequence: [1 2 4 5 6 9]
The second key to Hill sorting is how to obtain keywords, because other content andInsert sortSame
So how to select keywords? It is divided into three groups. What is the basis of this group? Why not two groups, six groups, or other groups?
Common Features of a good incremental sequence:
① The last increment must be 1
② Avoid the mutual multiples of values (especially adjacent values) in the sequence.
See http://baike.baidu.com/view/2217047.htm
There is no formula for such a critical problem, and only two criteria are given.
Okay. Generally, if there are 10 numbers to be arranged, you can select 5 3 1 in sequence. In other cases, you can only determine whether the above two "good" criteria are met.
That is to say, there is no rule for this key choice. You can choose either of them. There is only one keyword, And the keyword is getting smaller and smaller until 1.
Post-completion:
The increment value rule is to take half of the total length for the first time, take half of the half for the second time, and push until 1 in turn. I just saw this description in the following article. Thank you!
Algorithm series are quickly achieved in 15 days-seven classical sorting in the third day [bottom]
Back to main directory [classic Sorting Algorithm] [Collection]