Implementation of Hill Sort algorithm

Source: Internet
Author: User

  The Hill sort (Shell sort) is a kind of insertion sort, which is an improvement on the direct insertion sorting algorithm. This method is also known as narrowing the incremental sort, because of the DL. The shell was named after it was introduced in 1959.

The hill sort is essentially a grouping insertion method. Its basic idea is: for N to sort the series, take an integer less than n Gap (gap is called step) to sort the elements into several groups of sub-sequences, all the distance is a multiple of the gap is placed in the same group; then, the elements within each group are directly inserted into the sort. Once this sequence is complete, the elements of each group are ordered. The gap value is then reduced, and the grouping and sorting are performed repeatedly. Repeat this operation, when the gap=1, the whole sequence is orderly.

#include <iostream>using namespace std;void shellsort (int arr[],int n) {int i,j,gap,k,key;for (gap = n/2;gap > 0;g  AP/= 2) {for (i = 0;i < Gap;i + +) {for (j = i + gap;j < N;j + = gap) {k = J;key = arr[k];while (k-gap >= 0 && ARR[K] < Arr[k-gap]) {Arr[k] = arr[k-gap];k-= gap;} ARR[K] = key; }}}}int Main () {    int a[10] = {3,6,1,9,4,5,2,7,0,8};    Shellsort (a,10);    for (int i = 0;i < 10;i + +)        cout<<a[i]<< "";    cout<<endl;    return 0;}

  Operation Result:

Implementation of Hill Sort algorithm

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.