--java implementation of ranking series of Hill sort algorithm

Source: Internet
Author: User

Basic idea:

The essence of hill sort is grouping insertion sort, also known as narrowing increment method.

The entire unordered sequence is divided into a number of sub-sequences (consisting of elements separated by an "increment") to be directly inserted into the sort, then reduced incrementally and then sorted, and then once the elements in the whole sequence are basically ordered, a direct insertion of the entire element is ordered.

because the direct insertion sort is very efficient when the elements are basically ordered, the hill sort is much more efficient in time .

 Instance:

  Unordered sequence: int a[] = {3,1,5,7,2,4,9,6};

First trip: n=8; gap=n/2=4; The entire sequence is divided into 4 sub-sequences {3,2}, {1,4}, {5,9}, {7,6}

Second trip: gap=gap/2=2; The entire sequence is divided into 2 sub-sequences {2,5,3,9}, {1,6,4,7}

Third time: Direct insert sort for entire sequence

  

  The hill sort is unstable.

Java implementations:

 Packagesort;/*** Implementation of the Hill sorting algorithm *@authorThe ginkgo leaves of the season*/ Public classShellsort { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        NewShellsort (). run (); }    Private voidrun () {//TODO auto-generated Method Stub        intA[] = {3,1,5,7,2,4,9,6}; System.out.println ("——————————————————— Hill sorting algorithm —————————————————————");//Shellsort (a);ShellSort2 (a);    Printresult (a,a.length); }    /*** Hill Sort (reduced increment method) belongs to insert class sort * unstable *@parama*/    Private voidShellsort (int[] a) {        intn=a.length; intGap=n/2;  while(gap>=1){             for(inti=gap;i<a.length;i++){                intJ=0; inttemp =A[i];  for(J=i-gap;j>=0 && temp<a[j];j=j-Gap) {A[j+GAP] =A[j]; } a[j+GAP] =temp;            } printresult (A,a.length); Gap= GAP/2; }    }    /*** Strictly by definition to write the hill sort *@parama*/    Private voidShellSort2 (int[] a) {        intn=a.length; intI,j,k,gap;  for(gap=n/2;gap>0;gap/=2){             for(i=0;i<gap;i++){                         for(j=i+gap;j<n;j+=Gap) {                    inttemp =A[j];  for(K=j-gap;k>=0 && a[k]>temp;k-=Gap) {A[k+gap]=A[k]; } a[k+gap]=temp;        }} printresult (A,a.length); }    }    Private voidPrintresult (int[] A,intN) {         for(intj=0;j<n;j++) {System.out.print (" "+A[j]);    } System.out.println (); }}

Running results show:

  (This article is only for learning exchange, if there are better ideas, welcome to leave comments for everyone to explore learning ~) 

--java implementation of ranking series 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.