Fast sorting algorithm--java

Source: Internet
Author: User

A quick sort is to find an element (which can theoretically be arbitrarily found) as a datum (pivot), and then partition the array so that the values of the elements on the left of the datum are not larger than the datum, and the element values to the right of the datum are not less than the datum values, so that the elements of the datum are adjusted to the correct position after Recursive quick sorting, and other n-1 elements are also adjusted to the correct position after sorting. Finally, each element is in the correct position after sorting, and the sort is complete. So the core algorithm of the fast sorting algorithm is the partitioning operation, that is, how to adjust the position of the datum and adjust the final position of the return datum to divide and conquer the recursion.

For example, this may not be too good to understand. Suppose that the order to sort is listed as

2 2 4 9 3 6 7 1 5 First use 2 as the benchmark, using the I-j two pointers are scanned from both sides, separating the elements smaller than 2 and the elements larger than 2. First compare 2 and 5, 5:2, J shift Left

2 2 4 9 3 6 7 1 5 compare 2 and less than 2, so put 1 in the position of 2

2 1 4 9 3 6 7 1 5 Compare 2 and bis more than 2, so move 4 to the back

2 1 4 9 3 6 7 4 5 Compare 2 and 7,2 and 6,2 and 3,2 and 9, all greater than 2, meet the conditions, and therefore unchanged

After the first round of quick sorting, the elements change to look like this

[1] 2 [4 9 3 6 7 5]

After that, the 2 left side of the element is fast, because there is only one element, so the end of the fast line. The right side of the queue, recursive, and eventually produce the final result.

1  PackageCom.zc.manythread;2 3 ImportJava.util.Random;4 5 /**6 * Quick Sort7  * @authorAdministrator8  *9  */Ten  Public classQSort { One     int[] date; A      -      PublicQSort (int[] date) { -          the          This. date=date; -          -     } -     /** + * Swap function -      * @parama +      * @paramI A      * @paramJ at      */ -     Private voidSwapintA[],intIintj) { -         intT; -t=A[i]; -a[i]=A[j]; -a[j]=T; in     } -     /******************* to * Sorting function +      * @parama -      * @paramLo0 the      * @paramHi0 *      * @return $      */Panax Notoginseng     int[] QuickSort (intA[],intLo0,intHI0) {//divide and conquer the method, the function is divides the array into a[lo0. Q-1] and A[q+1..hi0] -         intlo=Lo0; the         intHi=hi0; +         intmid; A         if(hi0>Lo0) { themid=a[(hi0+lo0)/2]; +              while(lo<=hi) { -                  while((loLo; $                  $                  while((Hi>lo0) && (A[hi]>mid))--Hi; -                  -                 if(lo<=hi) { the swap (A,LO,HI); -++Lo;Wuyi--Hi; the                 } -                  Wu             } -             if(lo0<hi) { About QuickSort (A, lo0, HI); $             } -             if(lo<hi0) { - QuickSort (A, lo, hi0); -             } A         } +         returnA; the     } -     /************** $      *  the * Create duplicate array data the      * *****************/ the     Private Static int[] CreateDate (intcount) { the         int[] Data=New int[Count]; -          for(inti = 0; i < data.length; i++) { inData[i]= (int) (Math.random () *count); the         } the         returndata; About     } the     /** the * No duplicate array data the      * @paramCount +      * @return -      */ the     Private Static int[] CreateDate1 (intcount) {Bayi          the         int[] Data=New int[Count]; theRandom Rand =NewRandom (); -           Boolean[] bool =New Boolean[100]; -           intnum = 0; the            for(inti = 0; I < count; i++) { the             Do { the             //if the resulting number is the same, continue the loop thenum = rand.nextint (100); -} while(Bool[num]); theBool[num] =true; the          thedata[i]=num;94           } the           returndata; the     } the     /************** main function *****************/98      Public Static voidMain (string[] args) { About         Final intcount=10; -         int[] Data=createDate1 (count);101          for(intn:data) {102System.out.print (n+ "\ T");103         }104QSort data1=NewQSort (data); the System.out.println ();106         int[] a=data1. QuickSort (data,0, count-1);107          for(intn:a) {108System.out.print (n+ "\ T");109         } the     }111}

The results are as follows:

Fast sorting algorithm--java

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.