Data structure and algorithms--Hill sort (shell sort)

Source: Internet
Author: User

Hill sort (Shell sort)
#include <iostream>
using namespace Std;
void print (int a[], int n, int i)
{
Cout<<i << ":";
for (int j= 0; j<n; j + +)
{
COUT&LT;&LT;A[J] << "";
}
cout<<endl;
}
/* Direct insertion of the general form of the sort *
* d Zoom out increment, if is direct insert sort, d=1 */
void Shellinsertsort (int a[], int n, int d)
{
for (int i= D; i<n; ++i)
{
if (A[i] < a[i-d])//If the number of positions where I is greater than the number of i-d position, then directly inserted, otherwise the element moved, make space after inserting
{
int j = i-d;
int x = A[i]; Copy as Sentinel, which stores the elements to be sorted
while (x < a[j])//Find the insertion position in an ordered table
{
A[J+D] = A[j];
J-= D; Element move back
}
A[J+D] = x; Insert to correct position
}
Print (A, n,i);
}
}
/** The hill Sort by the increment D (N/2,n is the number of numbers to be sorted) first **/
void Shellsort (int a[], int n)
{
int d = N/2;
while (d >= 1)
{
Shellinsertsort (A, n, d);
D = D/2;
}
}
int main ()
{
int a[9] = {1,1,5,7,2,4,9,6,8};
int length=sizeof (a)/sizeof (a[0]);
Shellsort (a,length); Hill Insert Sort
Print (a,length,length);
}

Data structure and algorithms--Hill sort (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.