Sorting algorithm (ii)

Source: Internet
Author: User

Hill Sort ( unstable )


Algorithm ideas: (from small to large for example)

The hill sort uses an incremental sequence of H1,h2,h3,......, ht. With an incremental HK pair of data elements, all elements separated by HK have been ordered, i.e., for any position i,a[i]<=a[i+hk] constant. All elements remain in an orderly state after the order in which the increment is 1 is used.


Core Code   

Here the increment sequence takes: 1,3,5int hk;for (hk=5;hk>0;hk/=2) {for (i=hk;i<=n;i++) {int tmp=a[i];      Assigning elements greater than TMP to A[J-HK], thus avoiding direct exchange between elements, lifting speed for (J=HK;J-HK>=0;J-=HK) if (TMP<A[J-HK]) A[J]=A[J-HK];    else break;  a[j]=tmp; }}


Increment sequence:

1.Hibbard Incremental Sequence

2.Sedgewick Incremental Sequence

(Currently, I only have access to both of these delta sequences)


Complexity Analysis: (this part does not)

Time complexity: In the worst case, the time complexity is O (n^2); Other time complexity analysis relies on the selection of incremental sequences.

Spatial complexity: O (1) Space complexity



Heap Sort

(Knowledge of the heap is needed)

Algorithm idea:

Using the array storage element, the maximum binary heap is established, the maximum element is deleted, and the heap order property of the heap is guaranteed by filtering, and the maximal element is saved at the end of the array until the elements of the binary heap are all deleted; The elements in the array are sorted from small to large.


Core code:

#include <iostream>//This function is used to ensure the structural and sequencing properties of the heap void percdowm (int *a,int i,int n) {     int tmp;//tmp is used to store the value of the current root I     int child = 0;     for  (tmp = a[i]; 2 * i + 1 < n; i =  child) {child = 2 * i + 1;//Determines whether the subtree of node I exists, and if so, finds the maximum meta if  on the subtree (child ! =&NBSP;N&NBSP;-&NBSP;1&NBSP;&AMP;&AMP;&NBSP;A[CHILD]&NBSP;&LT;&NBSP;A[CHILD&NBSP;+&NBSP;1])  child++;if   (Tmp < a[child])  a[i] = a[child];else break;    }     a[i] = tmp;} Int main (void) {    elementtype a[200],tmp;    int n=0;     std::cin>>n;        for (int i=0;i <n;i++) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;STD::CIN&GT;&GT;A[I];&NBSP;&Nbsp;      //loops to construct the largest binary heap     for (i=n/2;i>=0;i--)    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PERCDOWM (a,i,n);     //Delete Max, re-construct Max Fork Heap      for (i=n-1;i>0;i--) {        tmp=a[0]; A[0]=a[i];a[i]=tmp;        percdown (A,0,i);     }         for (i=0;i<n;i++)          std::cout<<A[i]<< " ";    std::cout<<std::endl;         return 0;}


Analysis of Complexity:

Complexity of Time: O (N*LOGN)

Theoretically so, but actually the heap sort is slower than the hill sort


Sorting algorithm (ii)

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.