Shell sort based on insert Sort

Source: Internet
Author: User
Tags comparable

Reason:when It comes to insert sort,the snail Appears,as It just moves forward step by step or even worse. So we need some improvement.the first idea May is, we can walk forward in big strides. Shell sort do the same thing.

The basic realization (forward):

public static void Shellsort (comparable[] a) {
int len=a.length;
int STEPLEN=LEN/3;
Boolean flag=true;
int index=0;
while (flag) {
for (int i=0;i<steplen;++i) {
for (int j=i+steplen;j<len;j+=steplen) {
Insert Sort
for (int k=j;k>0;k-=steplen) {
if (less (A[k],a[k-steplen])) {
Swap (A,k,k-steplen);
}else{
Break
}
}
}
}
steplen/=3;
Make sure the last step length is 1
if (steplen==0) {
if (index>0) {
Flag=false;
}
Steplen=1;
++index;
}
}
}

The update realization (reverse):

public static void Updateshellsort (comparable[] a) {
int n=a.length;
int h=1;
while (H<N/3) h=3*h+1;
while (H >= 1) {
for (int i=h;i<n;++i) {
for (int j=i;j>=h && less (a[j],a[j-h]); j-=h) {
Swap (A,J,J-H);
}
}
H=H/3;
}
}

Shell sort based on insert Sort

Related Article

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.