Hill Sort method (Shell sort)

Source: Internet
Author: User

The hill Sort method (narrowing increment method) belongs to the sort of insert class, which is the method of dividing the whole sequence into several small sub-sequences to insert the sort separately. Hill Sort is a non-stable sorting algorithm. The method is due to DL. The shell was named after it was introduced in 1959.

The hill sort is based on the following two-point nature of the insertion sort, which proposes an improved method:
    1. The insertion sort is efficient in the case of almost sequenced data operations, i.e. the efficiency of linear sequencing can be achieved.
    2. But the insertion sort is generally inefficient because the insertion sort can only move data one bit at a time.

Hill sort is to group records by a certain increment of the subscript, sorting each group using the direct insertion sorting algorithm; As the increments gradually decrease, each group contains more and more keywords, when the increment is reduced to 1 o'clock, the entire file is divided into a group, the algorithm terminates.

First, take an integer less than n D1 as the first increment, grouping all the records in the file. All records with a multiple of D1 are placed in the same group. First, the direct insertion sort is performed within each group, and then the second increment d2<d1 repeats the above groupings and sorts until the increment =1 (< ... <d2<d1) is taken, that is, all records are placed in the same group for direct insert sorting. This method is essentially a grouping insertion methodComparing the number of distant distances (called increments) so that the number can move across multiple elements, it is possible to eliminate multiple element exchanges at once than [2]. D.l.shell realized this idea in 1959 in a sort algorithm named after him. The algorithm first sorts the group of numbers by an increment d into groups, each group of records of the subscript difference D. Sort all the elements in each group, then use a smaller increment to do it, and then sort them in each group. When the increment is reduced to 1 o'clock, the entire number to be sorted is divided into a group, and the sort is completed.The average initial fetch sequence is half- increment , and then halved each time until the increment is 1. StabilityBecause of the number of insertions, we know that one insert sort is stable and does not change the relative order of the same elements, but in different insertion sorts, the same elements may move in their own insert sort, and finally their stability will be disturbed, so the shell sort is unstable.Sorting ProcessDecrease IncrementThe hill sort belongs to the insertion class ordering, which divides the entire ordered sequence into several small sub-sequences for insertion sorting. Sorting process: First take a positive integer d1<n, put all the array elements separated by D1 in a group, the group is directly inserted into the sort, and then take D2&LT;D1, repeat the grouping and sorting operations, until the Di=1, that is, all records in a group to sort up.Algorithm AnalysisPros and consNo need for a lot of secondary space, and merge sort is as easy to implement. Hill sort is an algorithm based on the insertion sort, which adds a new feature to the algorithm and improves the efficiency. The time complexity of the hill sort is related to the selection of the increment sequence, such as the Hill increment time complexity O (n²), and the time complexity of the Hibbard increment for the hill sort is O (), and the lower bound of the hill sort time complexity is n*log2n. Hill sort does not have fast sorting algorithm fast O (n (logn)), so the medium size is good and the size of a very large data sort is not the best choice. But the algorithm is much faster than the O () complexity. And the hill sort is very easy to implement, the algorithm code is short and simple. In addition, the hill algorithm does not have much difference in the worst case and average execution efficiency, while fast sequencing performs poorly in the worst case scenario. Experts advocate that almost any sort of work at the beginning can be sorted by hill, and if it proves fast enough in practice, it will be changed to a more advanced sorting algorithm such as fast sorting. In essence, the Hill sort algorithm is an improvement of the direct insertion sorting algorithm, which reduces the number of copies and is much faster. The reason for this is that when the N value is large, the data items need to be sorted for a small number of times, but the data items are very long distances. When the n value decreases, each trip needs and moves more data, which is already close to their final position after sorting. It is the combination of these two situations that makes the hill sort efficiency much higher than the insert sort. The performance of the shell algorithm is very much related to the selected grouping length sequence. Only the specific sequence of records to be sorted, can accurately estimate the number of comparison of keywords and the number of objects moved. To figure out the relationship between the number of keywords and the number of moves and the incremental selection, and to give a complete mathematical analysis, it is still a mathematical problem.Time Performance1. The selection of the increment sequence the execution time of the shell sort depends on the increment sequence. Common characteristics of good incremental sequences: ① The last increment must be 1;② should try to avoid the case that the values in the sequence (especially the neighboring values) are multiples of each other. A lot of experiments have been done to give a better result: when n is larger, the number of comparisons and movements is between nl.25 and 1.6n1.25. 2. The time performance of the shell sort is better than the direct insert sort of the time performance is better than the reason for direct insertion sorting: ① The comparison and number of moves required to insert a sort directly when the initial state of the file is basically ordered is low. ② when the n value is small, the difference between N and is smaller, that is, the best time to insert the order of the complexity O (n) and the worst time complexity of 0 () is not very different. ③ in the beginning of the hill, the increment larger, more groups, the number of records in each group is small, so the direct insertion within the group is faster, and then the incremental di gradually reduced, and the number of groups gradually decreased, and the group of records gradually increased, but because already according to Di-1 as a distance arrangement, so that the file is closer to the orderly state So the new trip sort process is also faster. Therefore, the hill sort is more efficient than the direct insertion sort.Hill AnalysisHill sort is the insertion of elements according to different steps, when the first element is very disordered, the step is the largest, so the number of elements inserted in the order is very small, fast; When the elements are basically ordered, the step size is very low, and the insertion sort is very efficient for ordered sequences. So, the time complexity of hill sorting would be better than O (n^2).

Hill Sort method (Shell sort)

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.