1 ImportJava.util.Scanner;2 3 /*algorithm idea: Each trip will be a sort of element as a keyword, according to the size of the keyword value inserted into the order of the sequence of the appropriate position, until the insertion is complete,*/4 /*Average Time complexity O (n^2), Space complexity O (1)*/5 /*Features: Insert sort All is the last trip to complete without any record reaching its final position*/6 7 Public classZhijiecharusort {8 Public Static voidMain (String strg[]) {9Scanner cin =NewScanner (system.in);TenString str =cin.nextline (); OneString st[] = Str.split (""); A intc[]=New int[st.length]; - for(inti=0;i<st.length;i++){ -c[i]=Integer.parseint (St[i]); the } - Insertsort (c); - for(inti=0;i<st.length;i++){ - System.out.print (C[i]); +System.out.print (""); - } + cin.close (); A } at Public Static voidInsertsort (intr[]) { - inttemp; - intJ; - for(inti=1;i<r.length;i++){ -temp=R[i]; -J=i-1; in while(j>=0&&temp<R[j]) { -r[j+1]=R[j]; toj--; + } -r[j+1]=temp; the } * } $}
Java Implementation Direct Insert Lookup