1 /*binary Insert Find idea: Each trip with a sorted element as a keyword inserted into the appropriate position of the part sequence already arranged, find the appropriate location method with binary lookup method2 * Suitable for recording a large number of scenes3 * Save time when looking for insertion position4 * It is the same as the direct insert lookup in recording the number of moves, so the time complexity is still O (n^2)5 * Spatial complexity of the same as O (1)*/6 7 ImportJava.util.Scanner;8 9 Public classZhebancharusort {Ten One Public Static voidMain (String args[]) { AScanner cin =NewScanner (system.in); -String str =cin.nextline (); -String st[] = Str.split (""); the intNum[] =New int[st.length]; - for(inti=0;i<st.length;i++){ -num[i]=Integer.parseint (St[i]); - } + sort (num); - for(intj=0;j<st.length;j++){ + System.out.print (Num[j]); ASystem.out.print (""); at } - cin.close (); - } - Public Static voidSortintr[]) { - for(inti=1;i<r.length;i++){ - if(r[i]<r[i-1]){ in intLow=0; - intHigh=i-1; to inttemp =R[i]; + while(low<=High ) { - intMID = (int) ((Low+high)/2); the if(r[mid]<temp) { *Low=mid+1; $}Else{Panax NotoginsengHigh=mid-1; - } the } + for(intj=i;j>low;j--){ AR[j]=r[j-1]; the } +r[low]=temp; - } $ } $ } -}
Java implementation Binary (dichotomy) Insert sort