Quick Sort Java Implementation

Source: Internet
Author: User

 PackageKpp.sort;/*** Quick Sort * General selection of the first element as the pivot element, save to pivot, determine the head and tail pointer left,right, * First order the entire array element, when the elements on both sides of the pivot element ordered, and then the pivot element on each side of the two arrays to perform sorting * collation * 1. The right pointer iterates from the left, if it is smaller than the current pivot element pivot, assigns data[right] to Data[left] * The 2.left pointer traverses from left to right, if larger than the current pivot element pivot, assigns Data[left] to data[ Right] * 3. When the left and right pointers come together, such as index, Pivot is assigned to Data[index] * *@authorKPP **/ Public classQuickSort { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        intArray[] = {49,38,65,97,176,213,227,49,78,34,12,164,11,18,1}; QuickSort (Array,0, Array.length-1);  for(inti = 0; i < Array.Length; i++) {System.out.print (Array[i]+" "); }              }    Private Static intQuickSort (intData[],intLeftintRight ) {                intpivot; if(Left <Right ) {Pivot=sort (data,left,right);                        SYSTEM.OUT.PRINTLN (pivot); QuickSort (Data,left,pivot-1); QuickSort (Data,pivot+1, right); }        return0; }    Private Static intSortintData[],intLeftintRight ) {                intPivot =Data[left];  while(Left <Right ) {             while(Left < Right&&data[right] >=pivot) { Right--; } Data[left]=Data[right];  while(Left < Right&&data[left] <=pivot) { Left++; } Data[right]=Data[left]; }        //Left==right, to prove that the ancestral temple sorted, the pivot element is assigned to Data[left]Data[left] =pivot; returnLeft ; }    }

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.