插入排序

int[] array=new int[]{20,1,3,8,6,30,21,15};spa

System.out.println("Before order: ");it

for(int i=0;i<array.length;i++){new

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

}

int tmp;

int j;

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

tmp=array[i];

for(j=i-1;j>=0&&array[j]>tmp;j--){

array[j+1]=array[j];

}

array[j+1]=tmp;

}

System.out.println("\nAfter order: ");

for(int i=0;i<array.length;i++){

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

}

相關文章
相關標籤/搜索