public class ShellSort{
public static void main(String[] args){
int[] a = new int[20];
Random r =new Random();
for(int i = 0;i<20;i++){
a[i] = r.nextInt(100);
System.out.print (a[i]+" ");
}
System.out.println();
shellSort(a);
for(int num:a){
System.out.print(num+" ");
}
}
private static void shellSort(int[] a) {
int d = a.length/2;
for(;d>0;d--){
for(int j = 0;j<d;j++){
int temp = 0;
for(int n =j;n+d<a.length;n+=d){
if(a[n]>a[n+d]){
temp = a[n];
a[n] = a[n+d];
a[n+d] = temp;
}
}
}
}
}
}
希尔排序算法
最新推荐文章于 2024-11-18 08:41:09 发布