2 Java Hill Sort samples _java

Source: Internet
Author: User

Java Hill Sort

Hill sort is a type of insertion sort, or you can narrow the increment method with an image name. The basic idea is to divide an array into several arrays, a bit like partition, but the division here is controlled by a constant d.

This 0<d<n,n is the length of the array. This algorithm has the speed of insertion sort, also can be regarded as an improved algorithm, in the insertion algorithm, if there is a minimum number in the end of the array, the insertion algorithm will be heavier last

Position to the first, which would be wasteful, using this improved hill sort to achieve a large-span movement of data elements. That's the advantage of this algorithm.

Copy Code code as follows:

Package Cn.cqu.coce.xutao;

public class Shell3 {
public static void Main (String args[]) {
int a[]={7,43,23,5,3,2,0,6,74,9};
int n=a.length;
for (int i=0;i<n;i++)
System.out.print (a[i]+ "T");
System.out.println ();
for (int gap=n/2;gap>0;gap/=2) {
for (int i=gap;i<n;i++) {
for (int j=i-gap;j>=0&&a[j]>a[j+gap];j-=gap) {
int TEMP=A[J+GAP];
A[J+GAP]=A[J];
A[j]=temp;
}
}
}
for (int i=0;i<n;i++)
System.out.print (a[i]+ "T");
System.out.println ();
}
}



A second example

Copy Code code as follows:

Class Shell
{
public void Shell_sort (int [] arrays) {
for (int d=5;d>0;d=d-2) {
for (int c=0;c<arrays.length-d;c++) {
for (int i=c;i<arrays.length;i=i+d) {
for (int j=i;j>0;j=j-d) {
if (j<d)
Break
if (arrays[j]<arrays[j-d]) {
int tmp;
TMP=ARRAYS[J];
ARRAYS[J]=ARRAYS[J-D];
arrays[j-d]=tmp;

                        }
                    }
                }

            }
            SNP (arrays);
       }

   }
    public void SNP (int[ ] {
        for (int i=0;i<arrays.length;i++) {
   arrays)           System.out.print (arrays[i]+ "");

}
System.out.println ();
}
public static void Main (string[] args)
{
Shell s=new Shell ();
Int[] a={45,20,80,40,26,58,66,70};
S.shell_sort (a);

}
}

Run Result:

Copy Code code as follows:

----------Java----------
20 70 40 26 58 66 80
20 58 45 26 70 66 80
26 40 45 58 66 70 80

Output complete (time consuming 0 seconds)-Normal termination

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.