Quick sort and split sort

Source: Internet
Author: User

Quick sort let me look for a long time, also tortured me for a long period, because the general idea I have, but write code always appear all kinds of problems, want to debug it out for me personally still have a certain difficulty, and because of work and lazy reason, lead to debugging not come out of error put a long, today finally came out, There are some small excitement, oh, below to share my code and do a little bit of explanation.

To learn to sort quickly, you must first learn to divide and conquer the idea is to give a bunch of disorderly order of the number (the number is assumed, it can be other objects, of course, the parameters of the method can be defined by themselves oh, I suppose there is an integer array bar) and then give him an intermediate number, The division will divide the numbers into two parts, one on the left side of the middle number, the other on the right, and the number as the dividing point, and the number on either side is still out of order, and the method I define to him is as follows:

//left is the end of the array to divide the part, right is the total endpoint of the array division, such as an array of length 10, I want to divide the first 5 integers, then pass 0, 4 can     Public intSignalfenzhi (intLeftintRight ) {        if(left<0| | left>n-1| | right<0| | Right>n-1){            return-1; }        inttemp =Test[left]; intj=Right ; intI=Left ;  while(i<j) {             while(test[j]>=test[left]&&i<j) {J--; }             while(test[i]<=test[left]&&i<j) {i++; }                        if(i<j) {Temp=Test[i]; Test[i]=Test[j]; TEST[J]=temp; }        }                if(i==j) {Temp=Test[i]; Test[i]=Test[left]; Test[left]=temp; }                 for(intm=0;m<n;m++) {System.out.print (Test[m]+"     "); }                returni; }

Of course, it is possible to pass that intermediate number as a parameter, and now I'm simply using the left number of the array to pass in as the dividing number, just to illustrate.

Understand the division, then the rapid sequencing is simple, that is divided into two parts of the number and then the division, and so on, until all the numbers are in order, the code is as follows:

 Public voidQuickSort (intLeftintRight ) {        if(right-left<1){            return ; }Else{            intPoint = This. Signalfenzhi (left, right);            System.out.println (point); //if (point!=left&&point!=right) {QuickSort (left,point-1); QuickSort ( Point+1, right); //}        }    }

The efficiency of fast sequencing is excellent in many sorting algorithms, the time complexity is O (n*log2n), but if the dividing point of division is not good, the time complexity will be reduced to (the square of N), because if the array is ordered, then each time we take the number of the left end of the transmission, Then the efficiency will be very low, so to avoid this situation, if the detection of all the options, then it will take time, so a compromise approach, is to put the leftmost number and the right side of the number plus an intermediate number, find their three middle number, with this as the cutoff value will change a little better, on the basis of the above method, Modified after the code as follows, but I finished after the practice is not very good, should be the demarcation of the value also as passed to divide the method will be better, careful friend can try on their own, I do not try here, ha, is basically the same Oh!

 PackageCom.jll; Public classFenzhi {int[] test; intn=10;  PublicFenzhi () {test=New int[10];  for(inti=0;i<n;i++) {Test[i]=(int) (Math.random () *100) +1; System.out.print (Test[i]+"     ");    } System.out.println (); }         PublicFenzhi (intN) {        if(n>0){             This. n=N; Test=New int[n];  for(inti=0;i<n;i++) {Test[i]=(int) (Math.random () *100) +1; }        }    }         Public intSignalfenzhimajorizationfirst (intLeftintRight ) {        if(left<0| | left>n-1| | right<0| | right>n-1| | left>=Right ) {            return-1; }                if(right-left>=2){            intMiddle = (right+left)/2; if(test[left]>Test[middle]) {                inttemp =Test[middle]; Test[middle]=Test[left]; Test[left]=temp; }            if(test[left]>Test[right]) {                inttemp =Test[left]; Test[left]=Test[right]; Test[right]=temp; }            if(test[middle]>Test[right]) {                inttemp =Test[middle]; Test[middle]=Test[right]; Test[right]=temp; }            inttemp =Test[middle]; Test[middle]=Test[left]; Test[left]=temp; intJ=right-1; intI=left+1;  while(i<j) {                 while(test[j]>=test[left]&&i<j) {J--; }                 while(test[i]<=test[left]&&i<j) {i++; }                                if(i<j) {Temp=Test[i]; Test[i]=Test[j]; TEST[J]=temp; }            }            if(i==j) {Temp=Test[i]; Test[i]=Test[left]; Test[left]=temp; }                        /*if (i==j) {temp = Test[middle];                Test[middle]=test[i];            Test[i]=temp; }*/                        /*for (int m=0;m<n;m++) {System.out.print (test[m]+ ""); }*/                        returni; }Else {            if(test[right]<Test[left]) {                inttemp =Test[right]; Test[right]=Test[left]; Test[left]=temp; }            returnRight ; }    }         Public voidQuicksortmajorizationfirst (intLeftintRight ) {        if(right-left<1){            return ; }Else{            intPoint = This. Signalfenzhimajorizationfirst (left, right); System.out.println ("The point is:" +Point ); Quicksortmajorizationfirst (Left,point-1); Quicksortmajorizationfirst ( Point+1, right); }    }         Public Static voidMain (string[] args) {Fenzhi F=NewFenzhi (); System.out.println (F.signalfenzhimajorizationfirst (0, 9));        System.out.println (); F.quicksortmajorizationfirst (0,f.n-1); //F.quicksort (0,f.test.length-1);         for(inti:f.test) {System.out.print (i+" "); }    }}

The code runs as follows:

+ 1 The point is     :4of the Point was: 1 the point was:3 The point was:7The point was: 6The point is: 917 18 23 40 40 64 73 78 84 95

The above is what I learned, record, for later review

Quick sort and split sort

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.