An optimization for fast ordering of the same element aggregation method

Source: Internet
Author: User

The elements equal to par can be assembled at the end of a single split, and the next split will not be separated from par equal elements.

Example:

Sequence to sort 1 4 6 7 6 6 7 6 8 6

After partitioning, the results of equal treatment with par elements: 1 4 6 6 6 6 6 7 8 7

The next two sub-sequences are: 1 4 and 7 8 7

By comparison, we can see that, after a division, the elements equal to par are gathered together to reduce the number of iterations, and the efficiency will improve a lot

Process: In the process, there will be two steps

The first step, in the partitioning process, puts the key equal element into the two ends of the array

The second step, after dividing, moves the element equal to the key around par
//aggregating the same element method Packagesort;Importjava.util.Arrays;/** A quick sort of optimization **/ Public classTestQuickSort3 {//a quick row .     Public Static intPartion (int[]array,intLowinthign) {                inttmp=Array[low];  while(low<hign) {             while(lowtmp) {                --hign; }            if(low>=hign) {                 Break; }Else{Array[low]=Array[hign]; }             while(lowtmp) {                ++Low ; }            if(low>=hign) {                 Break; }Else{Array[hign]=Array[low]; }} Array[low]=tmp; returnLow ; }//Define aggregation methods Public Static int[] Focusnum (int[] Array,intStartintEndintParintLeftintRight ) {        intParleft=par-1; intParright=par+1; intTmp=0;
Left looking for(inti=par-1;i>=start;i--){ if(array[i]==Array[par]) { if(i!=parleft) {tmp=Array[parleft]; Array[parleft]=Array[i]; Array[i]=tmp; Parleft--; }Else{parleft--; } } }
Find on the right for(intj=par+1;j<=end;j++){ if(array[j]==Array[par]) { if(j!=parright) {tmp=Array[parright]; Array[parright]=Array[j]; ARRAY[J]=tmp; Parright++; }Else{parright++; }}} Right=parright;//A non-identical element on the far right of the same element that is clustered left=parleft;//A non-identical element on the leftmost side of the same element that is clusteredint[]brray=New int[2]; brray[0]=Left ; brray[1]=Right ; returnBrray; } Public Static voidQuick (int[]array,intStartintend) { intPar=partion (array,start,end); intLeft=par-1; intRight=par+1; int[]brray=Focusnum (array,start,end,par,left,right); Left=brray[0]; Right=brray[1]; if(par>start+1) {//We need a quick line on the left .Quick (Array,start,left); } if(par<end-1) {//We need a quick line on the right .Quick (array,right,end); } } Public Static voidQuickSort (int[]array] {Quick (array,0,array.length-1); } Public Static voidMain (string[] args) {//TODO auto-generated Method Stub int[]array={1, 4, 6, 6, 6, 6, 6, 7, 7, 8}; QuickSort (array); System.out.println (arrays.tostring (array)); }}

An optimization for fast ordering of the same element aggregation method

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.