shell排序——day4

18. April 2017 深度学习 0
//第四天,shell排序

public class shellSort_day4 {

staticfinalintMax=10;

public static void main(String[] args) {

// TODO Auto-generated method stub

int[] array1=new int[Max];

int i;

for(i=0;i<Max;i++){

array1[i]=(int)(100+Math.random()*100);//产生随机数组

}

System.out.println("排序前:");

for(i=0;i<Max;i++){

System.out.print(array1[i]+" ");

}

System.out.println("\n");

shellSort(array1);

System.out.println("排序后:");

for(i=0;i<Max;i++){

System.out.print(array1[i]+" ");

}

System.out.println("\n");

}

private static void shellSort(int[] array1) {

// TODO Auto-generated method stub

int i,j,temp,t;

for(i=array1.length/2;i>=1;i=i/2){

for(j=i;j<array1.length;j++){

t=j-i;

temp=array1[j];

while (t>=0 && temp<array1[t]) {

array1[t+i]=array1[t];

t=t-i;

}

array1[t+i]=temp; 

}

}

}

}