Java enables quick sorting

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.