Algorithm and sorting (4)

Source: Internet
Author: User

Shell sort is a sort of insert. It was named after D. l. Shell in 1959.

Basic Thinking of hill sorting


Basic Idea:

Take an integer D1 smaller than N as the first increment, and divide all records of the file into D1 groups. All records whose distance is a multiple of DL are placed in the same group. Sort directly inserted persons in each group first, and then take the second incremental D2 <d1 repeat the preceding grouping and sorting, until the incremental dt = 1 (dt <DT-L <... <D2 <d1), that is, all records are placed in the same group for direct insertion sorting.

This method is essentially a grouping insertion method.

Shell sorting process for a given instance

Assume that the file to be sorted has 10 records, and the keywords are:
49,38, 65,97, 13.

The incremental sequence values are as follows:
5, 3, 1

The sorting process is shown in [animation simulation demo ].

Shell Sorting Algorithm Implementation

1. Algorithm Description without monitoring record

Void shellpass (seqlist R, int D)

{// A sort in the hill sorting, and D is the current increment.

For (I = d + 1; I <= N; I ++) // insert R [d + 1 .. n] into the current ordered area of each group.

If (R [I]. Key <R [I-d]. Key ){

R [0] = R [I]; j = I-d;
// R [0] is only a temporary storage unit, not a sentry

Do {// find the Insert Location of R [I]


R [J + D]; = R [J]; // post-shift record


J = J-D; // search for the previous record

} While (j> 0 & R [0]. Key <R [J]. Key );

R [J + D] = R [0];
// Insert R [I] to the correct position

} // Endif

} // Shellpass

Void shellsort (seqlist R)

{

Int increment = N; // The initial incremental value. Set n> 0.

Do {

Increment = increment/3 + 1;
// Calculate the next increment

Shellpass (R, increment );
// Sort incremental shell inserts into the Increment

} While (increment> 1)

} // Shellsort
Note:

When incremental d = 1, shellpass and insertsort are basically the same, but a cycle criterion "j> 0" is added to the insertsort because there is no Sentel to prevent the subscript from crossing the border.

2. Set the shell Sorting Algorithm for monitoring record

Specific algorithms [bibliography [12]

Algorithm Analysis


1. incremental sequence Selection


The execution time of shell sorting depends on the incremental sequence.
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.

A large number of experiments have been conducted to show the current good results: WHEN n is large, the number of comparisons and moves is between nl.25 and 1.6n1.25.

2. Shell sorting has better time performance than direct insertion sorting
The reason why the time performance of hill sorting is better than that of direct insertion sorting:
① When the initial state of the file is basically ordered, the number of comparisons and moves required for direct insertion sorting is relatively small.
② When the N value is small, the difference between N and N2 is also small, that is, the best time complexity of direct insertion sorting O (N) and the worst time complexity 0 (N2) are not much different.
③ At the beginning of the hill sorting, there were a large increase in the number of groups and a small number of records in each group. Therefore, the number of records in each group was rapidly inserted. Later, the incremental di gradually reduced and the number of groups gradually reduced, the number of records in each group gradually increased, but because the di-1 has been used as the distance sorting order, the file is closer to the orderly state, so the new sorting process is also faster.
Therefore, the efficiency of hill sorting is much higher than that of direct insertion sorting.

3. Stability
Hill sorting is unstable. For more information, see the preceding example. In this example, the relative order of two identical keywords 49 changes before and after sorting.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.