Hill Sort algorithm implementation (1099)

Source: Internet
Author: User

Describe


The program implements the hill sorting algorithm, which, by non-descending ordering, tests the data as integers.

Input

The first line is the number of data elements to be sorted n;
The second line is the data element to be sorted.

Output

The result of a trip to the sort of hill.

Sample input

1050 36 41 19 23 4 20 18 12 22

Sample output

4 20 18 12 22 50 36 41 19 23

The Hill sort (Shell sort) is a sort of insertion. Also known as narrowing incremental sorting, is a more efficient and improved version of the direct insertion sorting algorithm. Hill Sort is a non-stable sorting algorithm. The method is due to DL. The shell was named after it was introduced in 1959. 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. [1] and the problem is a trip to the hill sort, that is, the sorting cycle is reduced to one time, the specific code is as follows #include<iostream> #include<algorithm> using namespace std; #define maxsize 1000 void shellSort( int *a, int n) {      int i,j,gap;      //for(gap=n/2;gap>0;gap/=2)      for (gap=n/2;gap==n/2;gap/=2)      {          for (i=gap;i<n;i++)          {              for (j=i-gap;j>=0&&a[j]>a[j+gap];j-=gap)              {                  swap(a[j],a[j+gap]);              }          }      } } int main() {      int n;      cin>>n;      int a[maxsize];      for ( int i=0;i<n;i++)      {          cin>>a[i];      }      shellSort(a,n);      for ( int i=0;i<n;i++)      {          cout<<a[i]<< " " ;      }      return 0; }

Hill Sort algorithm implementation (1099)

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.