Sort of quick sort (quickSort)

Source: Internet
Author: User
Tags benchmark

For example, an array

6,1,2,7,9,4,5,10,8
int a[]={6,1,2,7,9,4,5,10,8};

There are three elements in the fast line: Benchmark flag, left Sentinel I, right Sentinel

For convenience, the first digit of the array is used as the flag datum

Remember, each move is the right Sentinel J first move, J start probing from 8, looking for a number smaller than the benchmark 6, j--

I start probing from 6 to find a number larger than the benchmark 6, i++, swap

After the exchange, or from the right Sentinel J start probing, looking for a number smaller than the benchmark 6

Left Sentinel i++ probe larger than the base number, swap

After the exchange, or from the right Sentinel J start probing, looking for a number smaller than the benchmark 6, found 3

Left Sentinel i++ detection is larger than the reference number

Two sentinels meet, and they can't keep going.

The 3 and benchmark of the position are then exchanged at this time

At this point, the original sequence is divided into two sequences, the left sequence is 3 1 2 5 4 The right sequence is 9 7 10 8

The next step is to process the two sequences, starting with the left sequence 3 1 2 5 4

Benchmark for 3 continue top operation adjustment after 2 1 3 5 4

Left sequence is 2 1 adjusted after 1 2 3 5 4 6 9 7 10 8

Right sequence adjustment 1 2 3 4 5 9 7 8

Next deal with right sequence 9 7 10 8 the same way

The code is as follows

1  Public classQuickSort {2       3     /** 4 * Divide an array of elements, small on the left, large on the right5      * @parama6      * @paramStart7      * @paramEnd8      * @return 9      */  Ten      Public Static intDivideint[] A,intStartintend) {   One         //each time the rightmost element is used as the reference value A         intBase =A[end];  -         //when start equals end, it means that the left and right two pointers are merged into the same position and can end the round cycle.  -          while(Start <end) {   the              while(Start < end && A[start] <=base) -                 //traverse from the left, and if it is smaller than the base value, continue to the right. -start++;  -             //at the end of the while loop above, it means that the value of the current A[start] is larger than the base value and should be exchanged with the base value +             if(Start <end) {   -                 //Exchange +                 inttemp =A[start];  AA[start] =A[end];  atA[end] =temp;  -                 //after the swap, the swapped value at this point is also adjusted to the correct position (to the right of the reference value), so the right side also moves forward one -end--;  -             }      -              while(Start < end && A[end] >=base) -                 //traverse from the right, and continue to the left if it is larger than the base value . inend--;  -             //at the end of the while loop above, it means that the value of the current A[end] is smaller than the base value and should be exchanged with the base value to             if(Start <end) {   +                 //Exchange -                 inttemp =A[start];  theA[start] =A[end];  *A[end] =temp;  $                 //after the swap, the swapped value at this point is also adjusted to the correct position (to the left of the Datum), so the left side should also move back onePanax Notoginsengstart++;  -             }      the                +         }   A         //This returns either Start or end, where start and end are the datum values. the         returnend;  +     }   -    $     /**  $ * Sort -      * @parama -      * @paramStart the      * @paramEnd -      */  Wuyi      Public Static voidSortint[] A,intStartintend) {   the         if(Start >end) {   -             //If there's only one element, it doesn't have to be lined up anymore. Wu             return;  -         }    About         Else{   $             //If there is more than one element, continue to divide both sides recursively sort down -             intPartition =Divide (A, start, end);  -Sort (A, start, partition-1);  -Sort (A, partition+1, end);  A         }   +                the     }   -      Public Static voidMain (string[] args) { $          the         int[] B =New int[]{2,7,4,5,10,1,9,3,8,6};  the         int[] C =New int[]{1,2,3,4,5,6,7,8,9,10};  the         int[] D =New int[]{10,9,8,7,6,5,4,3,2,1};  the         int[] A =New int[]{1,10,2,9,3,2,4,7,5,6};  -                inSort (A, 0, a.length-1);  the                theSYSTEM.OUT.PRINTLN ("Sorted Result:");  About          for(intx:a) {   theSystem.out.print (x+ "");  the         }   the     }   +  -}

  

Sort of quick sort (quickSort)

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.