1 PackageExchange;2 3 ImportJava.util.Scanner;4 5 /*algorithm idea: A quick order is a "pivot", the sequence is divided into two parts, the pivot side is all smaller than it (or less than equal), one side is larger than it (or greater than equal). Then6 * Sort the two parts in the same way, sequentially, after a few such sorts, and finally get an ordered sequence. The first element is usually selected as the pivot. 7 * The closer the sequence is to the order, the higher the efficiency of the fast sorting, the more orderly and the lower the efficiency. 8 * The average time complexity is O (NLOG2 (n)) [2 is the bottom], in terms of average time, the fast sort is the best of all sorting algorithms, the number of fast sorted and the initial sequence is related9 * Space complexity O (log2 (n)) [2 is the bottom], the fast row is recursive, recursion needs the auxiliary of the stack. */Ten Public classKuaisusort { One A Public Static voidMain (string[] args) { -Scanner cin =NewScanner (system.in); -String str =cin.nextline (); thestring[] st = Str.split (""); - intC[] =New int[st.length]; - for(inti=0;i<st.length;i++){ -c[i]=Integer.parseint (St[i]); + } -Sort (c,0,c.length-1); + for(inti=0;i<c.length;i++){ A System.out.print (C[i]); atSystem.out.print (""); - } - cin.close (); - } - Public Static voidSortintR[],intLintR) { - inttemp; in inti = L,j =R; - if(l<R) { totemp = r[0]; + while(i!=j) { - while(j>i&&r[j]>temp) j--;//right-to-left traversal finds an element less than temp the if(l<j) { *r[i]=R[j]; $i++;Panax Notoginseng } - while(i<j&&r[i]<temp) i++;//walk from left to right to find elements greater than temp the if(i<j) { +r[j]=R[i]; Aj--; the } + } -R[i]=temp;//Put temp in the final position. $Sort (r,0,i-1);//recursively sorts the elements to the left of temp $Sort (r,i+1,j);//recursively sort the elements on the right side of temp - } - } the}
Java enables quick sorting