insertSort
作者:互联网
1 public static int[] insertSort(int[] arr){ 2 int i,j,idx,jdx,n,ndx,flag,fdx; 3 int[] brr=new int[arr.length]; 4 5 //in 1st loop,cannot execute the loop,so initialize the first element 6 brr[0]=arr[0]; 7 //loop start from 2nd element 8 for(i=2;i<=arr.length;i++){ 9 printArr(brr); 10 idx=i-1; 11 flag=1; 12 for(j=i-1;j>=1;j--){ 13 jdx=j-1; 14 if(arr[idx]>brr[jdx]) 15 {flag=j+1;break;} 16 17 18 19 } 20 fdx=flag-1; 21 22 //larger than flag,include flag,empty flag postion, push 1 postion 23 for(n= i-1;n>= flag;n--){ 24 ndx=n-1; 25 //push 1 postion; 26 brr[ndx+1]=brr[ndx]; 27 28 } 29 brr[fdx]=arr[idx]; 30 31 // V(j+1)=v(i); 32 33 34 35 36 } 37 return brr; 38 39 }
标签:arr,ndx,int,postion,brr,insertSort,flag 来源: https://www.cnblogs.com/lsjava/p/15536599.html