插入排序——day3

18. April 2017 深度学习 0
//第三天,插入排序

public class insertSort_day3 {

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");

insertSort(array1);

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

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

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

}

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

}

private static void insertSort(int[] array1) {

// TODO Auto-generated method stub

int i,j,temp;

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

temp=array1[i];

j=i-1;

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

array1[j+1]=array1[j];

j--;

}

array1[j+1]=temp;

}

}

}