Java code implementation for fast sorting

Source: Internet
Author: User

Quick Sort (Quicksort) is an improvement to the bubbling sort. Its basic idea is: by a trip to sort the data to be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.

Implementation method One:

1  Packagetest1;2 3  Public classQuicsort {4     /*5 * Use Quick sort6 * Arras: Array to sort7 * Low: Start subscript for array8 * Hight: The end subscript of an array9      */Ten      Public voidSortint[] Arras,intLowinthight) { One         inti =Low ; A         intj =hight; -         if(i>j) { -             return ; the         } -         //Datum elements -         intKey =Arras[low]; -          while(true){//Let's compare all the elements in a trip. +             //J, go ahead. -              while(j>i) { +                 if(Arras[j] <key) { A                     //Exchange at                     inttemp =Arras[j]; -ARRAS[J] =Arras[i]; -Arras[i] =temp; -                      Break; -}Else{ -j--; in                 } -             } to             //I go backwards +              while(j>i) { -                 if(Arras[i] >key) { the                     //Exchange *                     inttemp =Arras[j]; $ARRAS[J] =Arras[i];Panax NotoginsengArras[i] =temp; -                      Break; the}Else{ +i++; A                 } the             } +             if(i = =j) { -                  Break; $             } $         } -         //sort the array to the left of the Datum -Sort (arras,low,i-1); the         //array ordering to the right of the Datum -Sort (arras,i+1, hight);Wuyi     } the     /* - * Print the elements inside the array Wu      */ -      Public voidPrintint[] Arras) { About          for(inti=0; i<arras.length;i++){ $             if(i==arras.length-1){ - System.out.println (Arras[i]); -}Else{ -System.out.print (arras[i]+ ","); A             } +         } the     } -      Public Static voidMain (string[] args) { $         int[] as = {49,38,65,97,76,13,27}; theQuicsort QS =NewQuicsort (); theQs.sort (as,0,as.length-1); the Qs.print (AS); the     } -}

Implementation method Two:

1  Packagetest1;2 3  Public classQuickSort1 {4     intA[] = {49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99,598, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };6      PublicQuickSort1 () {7 Quick (a);8          for(inti = 0; i < a.length; i++)9 System.out.println (A[i]);Ten     } One      Public intGetmiddle (int[] list,intLowintHigh ) { A         intTMP = List[low];//the first of the arrays as a middle axis -          while(Low <High ) { -              while(Low < High && List[high] >=tmp) { thehigh--; -             } -List[low] = List[high];//Less than mid-axis records moved to the low end -              while(Low < High && List[low] <=tmp) { +low++; -             } +List[high] = List[low];//a record larger than the middle axis moves to the high end A         } atList[low] = tmp;//Middle axis record to tail -         returnLow//returns the position of the middle axis -     } -      Public void_quicksort (int[] list,intLowintHigh ) { -         if(Low <High ) { -             intMiddle = getmiddle (list, low, high);//divides a list array into a split in_quicksort (list, low, middle-1);//recursive ordering of low-word tables -_quicksort (list, middle + 1, high);//recursive ordering of high-character tables to         } +     } -      Public voidQuickint[] A2) { the         if(A2.length > 0) {//to see if an array is empty *_quicksort (A2, 0, A2.length-1); $         }Panax Notoginseng     } -}

Java code implementation for fast sorting

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.