Quick Sort Java Implementation

Source: Internet
Author: User

Fast sorting is a sort algorithm based on the divide-and-conquer strategy. The basic idea is that for the input word group a[p:r],

Sort by the following 3 steps:

1. Decomposition(divide): ToA[p]for a datum element, theA[p:r]divided into3segmentA[p:q-1],A[q],A[q+1:r], makingA[p:q-1]is less than or equal to any element inA[q],A[q+1:r]any element greater than or equal toA[q]. subscriptQdetermined during the partitioning process.

2, Recursive solution (conquer) : The quick sort algorithm is called recursively, respectively, to a[p:q-1] and a[q+1:r] sort.

3. Merging (merge): Because the sort of a[p:q-1] and A[q+1:r] is in-place, the a[p:q-1 ] and A[q+1:r] are already in order and do not need to perform any calculations,A[p:r] has been sequenced. (In fact, after the array is decomposed,A[q] is the a[q of the final arrays ), which means that each decomposition determines an element in an ordered array, Always recursive until only one element is left and no further decomposition is required, and this element is the final result.

1  Public classQuickSort {2     3      Public Static inta[]= {1,4,3,8,6,0,2,5};4     5      Public Static voidQSort (intPintR) {6         if(p<R) {7             intQ = partition (P,R);//A[q] is the result of the last sort8QSort (p,q-1);//sort the left half of the paragraph9QSort (Q+1,R);//sort the right half of the paragraphTen         } One     } A      -     Private Static intPartitionintPintr) { -         intI=p, theJ=r+1; -         intx=A[p]; -         //swap elements that are less than X to the left area -         //swap elements greater than x to the right area +          while(true){ -              while(A[++I]&LT;X&AMP;&AMP;I&LT;R);//++i, so the [P+1,r] is sorted, and finally the P is inserted +              while(a[--j]>x); A             if(I&GT;=J) Break; at swap (A,I,J); -         } -         /* - * A[j] swapped with a[p] instead of a[i] because the above cycle ends, A[j]<=a[p]<=a[i] - * While P is the first element (that is, the left half of the group (less than or equal to P)) -          */ ina[p]=A[j]; -a[j]=x; to         returnJ; +     } -  the     Private Static voidSwapint[] A,intIintj) { *         inttemp=A[i]; $A[i] =A[j];Panax NotoginsengA[J] =temp; -     } the  +      Public Static voidMain (string[] args) { AQSort (0,a.length-1); the          for(inti=0;i<a.length;i++) +System.out.print (a[i]+ ""); -     } $  $}

In fact, never understand partition finally why will a[j] and A[p] Value exchange, later only want to understand, because partition is based on A[P] as the benchmark divided into two groups,

The left half of the final result is less than or equal to a[p], the right half is greater than or equal to a[p],

That is, in the final ordered array, A[p] is in the middle of the left and right halves, so a[p] is inserted in the middle of them,

The reason for choosing J instead of I is that the while (true) loop, A[j]<=a[p]<=a[i], and A[p] is the first element of the entire array (the left half),

So after the exchange can still guarantee that the left half is smaller than a[p], the right half is larger than the a[p].

Quick Sort 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.