Fast-sorting Java implementation (key location can be taken)

Source: Internet
Author: User

1 /**2  * @authorCody3  */4  Public classQuickSort {5      Public Static voidMain (string[] args) {6         int[] Array = {49,38,65,97,76,13,13,27,4,8,2,3,56};7QuickSort (array, 0, array.length-1);8          for(inti = 0; i < Array.Length; i++) {9 System.out.println (Array[i]);Ten         } One     } A  -     /** -      * @paramnums array to be recursively processed the      * @paramleft border begin 0 -      * @paramright Border end length-1 -      */ -     Private Static voidQuickSort (int[] Nums,intLeftintRight ) { +         intx = (left+right)/2; -         intKey =Nums[x]; +         inti =Left ; A         intj =Right ; at         intindex =x; -         //If the length is one or out of bounds, no processing is required -         if(left >= right)return; -          while(I <j) { -             //left to skip all >=key values -              while(I < J && Nums[j] >=key) { inj--; -             } to             //on the right, skip all <key values. +              while(I < J && Nums[i] <key) { -i++; the             } *             //use Index to save the array subscript where the key value is located $             //the corresponding value of x will not be changed or swapped oncePanax Notoginseng             if(i = = x && i! =j) { -index =J; the             } +             if(j = = x && i! =j) { Aindex =i; the             } +             //exchange of the corresponding values of I and J -             intTMP =Nums[i]; $Nums[i] =Nums[j]; $NUMS[J] =tmp; -         } -         //determine whether the value of i = = J (boundary) is greater or less than 0, divided into different places the         if(Nums[i] <key) { -++i;Wuyi         } the         inttemp =Nums[i]; -Nums[i] =Nums[index]; WuNums[index] =temp; -QuickSort (nums,left,i-1); AboutQuickSort (nums,i+1, right); $     } -}

Fast-sorting Java implementation (key location can be taken)

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.