A fast sorting algorithm for Java implementation

Source: Internet
Author: User

Quick Sort principle: Select a key value as the base value. Smaller than the baseline values are in the left sequence (usually unordered), which is larger than the base value on the right (usually unordered). The first element of a general selection sequence.

One cycle: from backward to forward, compare with the base value and the last value, if the swap position is smaller than the base value, if you do not continue to compare next, until you find the first value that is smaller than the base value is swapped. When this value is found, it is compared from the point of travel, if there is a larger than the base value, the swap position, if not continue to compare the next, until the first to find a value larger than the base value is exchanged. Until the previous comparison index > index from backward to forward, ending the first cycle, at which point the left and right sides are ordered for the reference value.

Then we compare the sequence of the left and right sides, repeating the above cycle.

 Public classfastsort{ Public Static voidMain (String []args) {System.out.println ("Hello World"); int[] A = {12,20,5,16,15,1,30,45,23,9}; intStart = 0; intEnd = A.length-1;        Sort (a,start,end);  for(inti = 0; i<a.length; i++) {System.out.println (a[i]); }             }           Public voidSortint[] A,intLowintHigh ) {         intStart =Low ; intEnd =High ; intKey =A[low];  while(end>start) {             //compare from backward to forward              while(End>start&&a[end]>=key)//If there is nothing smaller than the key value, compare next until there is a smaller swap position than the key value, and then compare it back to the previousend--; if(a[end]<=key) {                 inttemp =A[end]; A[end]=A[start]; A[start]=temp; }             //from the post-trip comparison              while(End>start&&a[start]<=key)//if there is no larger than the key value, compare next until there is a larger swap position than the key valuestart++; if(a[start]>=key) {                 inttemp =A[start]; A[start]=A[end]; A[end]=temp; }         //at this point the first loop is finished, and the key value position is determined. The values on the left are smaller than the key values, the values on the right are larger than the key values, but the order of the two sides may be different, making the following recursive calls         }         //Recursive         if(start>low) sort (a,low,start-1);//left sequence. First index position to key value index-1         if(end//right sequence. Index from key value +1 to last     }     }        

The last sentence above is not the base value, which means that it is not exchanged directly with the reference value, but with the index of the base value.

A fast sorting algorithm for Java implementation

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.