Sorting algorithm Analysis "Two": Hill sort (with python&c++ code)

Source: Internet
Author: User

Hill Sort

Also known as the descending incremental sorting algorithm , is a more efficient and improved version of insertion sequencing. Hill Sort is a non-stable 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 of the order in the almost already ordered data operation, high efficiency, that can achieve the efficiency of linear sequencing ;

2, the insertion sort is generally inefficient, because the insertion sort can only move the data one bit at a time;

algorithm principle

The basis is the insertion sort, we first look at the picture, the image dynamic display:

This picture is too fast or I don't know what he's doing, giving a picture (pictures are from the Internet):

Step Selection

The choice of step in hill sort is the heaviest! "From Wikipedia"

1, the final step must be 1, any step serial can work. The algorithm starts with a certain step sequence. It then continues to sort by a certain step, and the final algorithm sorts with a step of 1. when the step is 1 o'clock, the algorithm becomes an insert sort, which guarantees that the data must be sorted.

2, the initial step selection: Donald Shell initially suggested that the step size is selected and the step is taken half until the step size reaches 1. Although this can be better than the algorithm of the class (insert sort), there is still room to reduce the average time and the worst time.

The most important thing about Hill sorting is that when sorted in smaller steps, the larger steps used previously are still ordered. For example, if a sequence is sorted in step 5 and then sorted by step 3, then the sequence is not only ordered in step 3, but also in step 5 order. If this is not the case, then the algorithm will disrupt the previous order during the iteration, and it will not finish the sort in such a short time.

The best known step-length serial is presented by Sedgewick (1, 5, 9, 109,...), the serial entries are derived from the two formulas, 4^i-9 * 2^i + 1 and 4^i-3 * 2^i + 1. The study also shows that "comparisons are the most important operation in hill sorting, not Exchange." "Hill sorting in such a stride 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."

The other step in the large array is excellent in the length of the serial is (Fibonacci series minus 0 and 1 will be the remaining number in the Golden partition ratio of twice times the power of the calculation of the sequence): (1, 9, 34, 182, 836, 4025, 19001, 90358, 428481, 2034035, 9651787, 45806244, 217378076, 1031612713, ... )

Graphic

2: Step is 3 o'clock, we grouped the data into: a[1], a[4], a[7] and a[2], a[5], a[8] and a[3], a[6], a[9], we respectively to the three groups to insert the sort;

Step is 2 o'clock, we continue to group as: A[1], a[3], a[5], a[7], a[9] and a[2], a[4], a[6], a[8], we respectively on these two groups of sorts;

The step is 1 o'clock for a true insertion sort, but the actual time complexity is less than the original insert sort because of the amount of data in order.

The algorithm implements the Python implementation:

#-*-encoding:utf-8-*-def Shell_sort (ST_BF):    # Hill sort    lens = Len (ST_BF)/2    steps = [Lens] While    (Lens > 1):        lens/= 2        steps.append (lens) for        step in steps:        print ' steps = {} '. Format (step) for        I in xrange (s TEP, Len (ST_BF)):              temp = st_bf[i]              j = i-step while             J >= 0 and St_bf[j] > Temp:                  st_bf[j+step] = ST_BF[J]                    J-= Step              St_bf[j+step] = temp              print st_bf                ST_BF = [6, 5, 3, 1, 8, 7, 2, 4, 2]shell_sort (st_b F

The result is:

C + + implementation:

#include <iostream>using namespace std;void print (int st_bf[], size_t size) {for (size_t i = 0; i < size; i++    ) {cout<< st_bf[i] << ""; } cout<< Endl;} void Insert_sort (int st_bf[], size_t size) {//Insert sort code for (size_t i = 1; i < size; ++i) {int temp = St        _bf[i];        int j = i-1;            while (J >= 0 and St_bf[j] > Temp) {st_bf[j+1] = st_bf[j];        J-= 1;        } st_bf[j+1] = temp;    Print (st_bf, size);         }}void shell_sort (int st_bf[], size_t size) {//pseudo-Hill Sort code//K for step for (size_t k = size/2; k > 1; k = K/2) {        cout << "k =" << k << Endl;            I < K play the role of the group Sentinel, otherwise repeat the comparison j< size to avoid crossing for (int i = 0, j = i+k; I < K and J < size; ++i, ++j) {  for (int m = i, n = j; n < size;    M + = k, n + = k) {cout << "i =" << m << ", j =" << n << Endl;            cout << st_bf[m] << "<< st_bf[n] << Endl;                    if (St_bf[m] > St_bf[n]) {int temp = St_bf[n];                    St_bf[n] = st_bf[m];                ST_BF[M] = temp;    }}} print (ST_BF, size);    }//n = 1 Normal insertion sort cout << "k = 1" << Endl; Insert_sort (ST_BF, size);} void Shellsort (int *st_bf, size_t size)//Real Hill Sort {for (size_t k = size/2; k >= 1; k = k/2) {cout <&lt ;        "k =" << k << Endl;            for (int i = k; i < size; ++i) {int temp = St_bf[i];            int j = i-k;                while (J >= 0 and St_bf[j] > Temp) {st_bf[j+k] = st_bf[j];            J-= k;        } St_bf[j+k] = temp;    } print (st_bf, size); }}int Main () {int st_bf[] = {6, 5, 3, 1, 8, 7, 2, 4, 2};size_t size = sizeof (ST_BF)/sizeof (st_bf[0]); insert_sort(ST_BF, size); cout << "===========" << endl;int st_bf1[] = {6, 5, 3, 1, 8, 7, 2, 4, 2};size = sizeof (ST_BF1)/si    Zeof (St_bf1[0]);    Shell_sort (st_bf1, size);    cout << "===========" << Endl;    int st_bf2[] = {6, 5, 3, 1, 8, 7, 2, 4, 2};    Shellsort (st_bf2, size); return 0;}

The result is ibid.

References

Click to open link

This article by @the_third_wave (blog address: http://blog.csdn.net/zhanh1218) original. There are not covered, will be updated periodically, there are errors please correct me.

If you see this blog post is not incomplete, that is I to prevent the crawler to release the first half of the reason, please see the original author blog.

If this blog post is helpful to you, for a good network environment, do not recommend reprint, Recommended Collection! If you must reprint, please bring the suffix and the address of this document.

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.