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