Hill sort (insert sort)-eight big sort three big find summaries (5)

Source: Internet
Author: User

Basic ideas

The basic idea of this method is that the whole sequence of elements is divided into several sub-sequences (consisting of elements separated by an "increment"), then the direct insertion sort is done separately, then the increment is reduced and then sorted, and then the whole element is sorted by a direct insertion when the elements in the entire sequence are basically ordered (the increment is small enough).

Stability

Because 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.

Complexity of Time

The time complexity of the hill sort depends on the choice of step size.

On average, the time complexity of hill sequencing is O (nlog2n), and the worst case is O (N1.5).

Complexity of space

O (1)

Usage Scenarios

The Nether of 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 real-world use, then change it to a more advanced sort algorithm like fast sorting.

Comparison

The essence of the hill sort is the grouping insert sort, which is also known as narrowing the incremental sort, which is a more efficient and improved version of the direct insertion sorting algorithm.

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.

The hill sort is better than the direct insertion sort, because the hill sort is inserted according to the different steps of the element, when the first element is very disorderly, 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 efficient for ordered sequences.

About selection of step (increment)

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. The best known step sequence is presented by Sedgewick (1, 5, +, 109,...), the sequence of entries fromthese two formulas.

The study also shows that "comparisons are the most important operation in hill sorting, not Exchange." "Hill sorting in such a step sequence is faster than inserting sort and heap sorting, even faster than fast sorting in a decimal group, but Hill sorting is slower than fast sorting when large amounts of data are involved."

Code and analysis

An array of n=10 49, 38, 65, 97, 26, 13, 27, 49, 55, 4 for example

First time gap = 10/2 = 5

49 38 65 97 26 13 27 49 55 4

1 a 1B

2 a 2B

3 A 3B

4 a 4 b

5 a 5B

1A,1B,2A,2B are grouped, the numbers are the same in the same group, the uppercase letters represent the first elements of the group, and each time the data for the same group is inserted directly into the sort. It is divided into five groups (49, 13) (38, 27) (65, 49) (97, 55) (26, 4) so that each group is sorted (13, 49) (27, 38) (49, 65) (55, 97) (4, 26), the same as below.

Second time gap = 5/2 = 2

After sorting

13 27 49 55 4 49 38 65 97 26

1 a 1 b 1C 1D 1E

2 a 2B 2C 2D 2E

The third time gap = 2/2 = 1

4 26 13 27 38 49 49 55 97 65

1 a 1 b 1C 1D 1E 1F 1G 1H 1I 1J

Fourth time gap = 1/2 = 0 Sort Done to get the array:

4 13 26 27 38 49 49 55 65 97

Here's a sort of hill that is written strictly by definition.

1 voidShellsort1 (intA[],intN)2 {3     intI, J, Gap;4 5      for(GAP = n/2; Gap >0; Gap/=2)//Step Size6     {7          for(i =0; I < gap; i++)//Direct Insert Sort8         {9              for(j = i + Gap; j < n; j + =Gap)Ten                 if(A[j] < a[j-Gap]) One             { A                 inttemp =A[j]; -                 intK = J-Gap; -                  while(k >=0&& A[k] >temp) the                 { -A[k + gap] =A[k]; -K-=Gap; -                 } +A[k + gap] =temp; -             } +         } A     } at}

Similar to the direct insert sort, simplify the above code and merge the two steps of search and data back.

1 voidShellsort2 (intA[],intN)2 {3     intJ, Gap;4     5      for(GAP = n/2; Gap >0; Gap/=2)6          for(j = Gap; J < N; j + +)//start with the gap element of the array7             if(A[j] < A[J-GAP])//each element is directly inserted into the sort with the data in its own group8             {9                 inttemp =A[j];Ten                 intK = J-Gap; One                  while(k >=0&& A[k] >temp) A                 { -A[k + gap] =A[k]; -K-=Gap; the                 } -A[k + gap] =temp; -             } -}

The following code simplifies the code for the hill sort, but reduces the efficiency

1 voidSHELLSORT3 (intA[],intN)2 {3     intI, J, Gap;4 5      for(GAP = n/2; Gap >0; Gap/=2)6          for(i = gap; i < n; i++)7              for(j = i-gap; J >=0&& A[j] > A[j + gap]; J-=Gap)8Swap (A[j], a[j +Gap]);9}

Main reference: http://blog.csdn.net/morewindows/article/details/6668714

Finally, Http://www.cnblogs.com/huangxincheng/archive/2011/11/20/2255695.html gives a comparison of the time performance of the direct insert sort and the hill sort (in C #).

Hill sort (insert sort)-eight big sort three big find summaries (5)

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.