Java Quick Sort

Source: Internet
Author: User

The code is as follows:

 Public classQuickSort {//exchanging elements at index I and J two in the data array    Private Static voidSwap (datawrap[] data,intIintj) {Datawrap tmp=Data[i]; Data[i]=Data[j]; DATA[J]=tmp; }    //the subsequence of the Start-end index range in the data array is processed so that all the less than the demarcation is worth on the left//all greater than the demarcation is worth putting on the right    Private Static voidSubsort (Datawrap [] data,intStartintend) {        //need to sort        if(Start <end) {            //take the first element as a cutoff valueDatawrap base =Data[start]; //I search from the left, searching for an index of an element greater than the cutoff value            inti =start; //I search from the left, searching for the index of the element that is less than the cutoff value            intj = End +1;  while(true)            {                //find the index of an element greater than the cutoff value, or I have found end                 while(I < end && Data[++i].compareto (base) <=0); //Find the index of the element that is less than the cutoff value, or J has found the start place                 while(J > Start && data[--j].compareto (base) >=0); if(I <j) {Swap (data, I, j); }                Else                {                     Break;            }} swap (data, start, j); //recursive left dial hand sequenceSubsort (data, start, j-1); //recursive right sub-sequenceSubsort (data, j+1, end); }                    }     Public Static voidQuickSort (Datawrap [] data) {subsort (data,0, Data.length-1); }     Public Static voidMain (string[] args) {datawrap[] data= {                NewDatawrap (21, ""),                NewDatawrap (30, ""),                NewDatawrap (49, ""),                NewDatawrap (30, ""),                NewDatawrap (16, ""),                NewDatawrap (9, "")        }; System.out.println ("Before sorting: \ n" +java.util.Arrays.toString (data));        QuickSort (data); System.out.println ("After sorting: \ n" +java.util.Arrays.toString (data)); }}

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