import java.sql.Array;
import java.util.Arrays;
public class shellSort {
public static <T extends Comparable<? super T>> void sort(T[] a, int first,
int last) {
int n = last - first + 1;
for (int space = n / 2; space > 0; space /= 2) {
for (int begin = first; begin < first + space; begin++)
incrementalSort(a, begin, last, space);
//System.out.println(Arrays.toString(a));
}
}
public static <T extends Comparable<? super T>> void incrementalSort(T[] a,
int first, int last, int space) {
int unsorted,index;
for(unsorted =first+space;unsorted<=last;unsorted+=space)
{
T firstUnsorted =a[unsorted];
for(index=unsorted-space;(index>=first)&&(firstUnsorted.compareTo(a[index])<0);index-=space)
{
a[index+space]=a[index];
}
a[index+space]=firstUnsorted;
}
}
public static void main(String[] argv)
{
Integer a[]={12,3,53,2,2,3,1,34,23};
System.out.println(Arrays.toString(a));
sort(a, 0, 8);
System.out.println(Arrays.toString(a));
}
}
java shell排序
最新推荐文章于 2021-09-16 09:04:12 发布