學(xué)習(xí)還是要多動手才好械荷,看十遍不如寫一遍,喜歡的可以復(fù)制代碼版仔。
public class SortUtil{
public static int[]insertSort(int[] a){
for(int i=1;i
//從數(shù)組的第二位開始循環(huán), a[j]與之前有序數(shù)組循環(huán)進(jìn)行比較
? ? ? ? ? ? for(int j=i;j>0 && a[j]>a[j-1];j--){
int temp = a[j];
? ? ? ? ? ? ? ? a[j]=a[j-1];
? ? ? ? ? ? ? ? a[j-1]=temp;
? ? ? ? ? ? }
}
return a;
? ? }
public static void main(String[] args) {
int [] arr ={3,9,2,8,4,7,6,5,1};
? ? ? ? int[] b =insertSort(arr);
? ? ? ? for(int a:b){
System.out.print(a);
? ? ? ? }
}
}