Quick Sort Quicksort

Source: Internet
Author: User

The first few days realized the direct insertion sort, the bubbling sort and the direct selection sort of the three basic sorts. Today we have a look at the improved algorithm for bubbling sorting, fast sorting. Record it separately, followed by merge and cardinality sorting, etc.

Quick Sort

1. Select a fulcrum the default is the first element of the array and Array[start] as a fulcrum, all values greater than the fulcrum element are placed behind the fulcrum, all values less than the fulcrum element are placed in front of the fulcrum, complete a division, return to the position of the Fulcrum index

2. Through a division to form two sequences with sequencing, [Start, index-1] and [index + 1, end] Two, in the execution of 1 until the sequence element only 1 have finished sorting

Specific explanations and notes are noted in the code comments

Quicksort.java

1 Package Com.gxf.sort;2 3 /**4 * This allows for quick sorting5 * Quick Sort is an improvement to bubble sort6 * Select an element in the array as the fulcrum, usually the first element7 * Place elements that are greater than the pivot point behind the fulcrum, placing elements smaller than the fulcrum in front of the fulcrum8 * Such a trip down, the fulcrum front is less than the fulcrum element, the fulcrum behind the greater than the Fulcrum element9 * Each trip is divided to return the position of the fulcrum, thus forming two arrays with a sort, recursively invoking two arrays respectivelyTen * until you divide only one element at a time One * @author Administrator A  * -  */ -  Public classQuickSort { the     /** - * Here is a trip to the array, which returns the position of the fulcrum in the arrays, and the default first element is the Fulcrum - * @param Rec - * @param start + * @param end - * @return +      */ A      Public intQuickSort (Rec rec[],intStartintend) { at         intFulcrum = Rec[start].key;//Fulcrum The fulcrum of the search -         inti =start; -         intj = END;//point to start and end respectively -          while(I < J) {//i==j Time to end a division -              while(I < J && Rec[j].key > Fulcrum)//find the element on the right that is smaller than the Fulcrum -j--; inRec[i].key = Rec[j].key;//move to the pivot position between the elements on the right . -              while(I < J && Rec[i].key < Fulcrum)//find the element on the left that is larger than the Fulcrum toi++; +Rec[j].key = Rec[i].key;//the left is greater than the pivot element to exchange the past -         } the         //consider the issue of equal elements here. *Rec[i].key =Fulcrum; $         returni;Panax Notoginseng     } -      the     /** + * Fast sorting of arrays A * Recursive Implementation the * @param rec +      */ -      Public voidSort (Rec rec[],intStartintend) { $         intindex; $         if(Start <end) { -index =QuickSort (REC, start, end); -Sort (REC, start, index-1); theSort (REC, index +1, end);//Note that this is going to be +1 or infinite recursion, causing the stack to overflow. -         }Wuyi     } the     /** - * @param rec Wu      */ -      Public voidsort (Rec rec[]) { AboutSort (REC,0, Rec.length-1); $     } -      -}

Rec.java

1  PackageCom.gxf.sort;2 3  Public classRec {4      Public intkey;5     6      PublicRec (intkey) {7          This. Key =key;8     }9      PublicRec () {Ten          One     } A      Public voidShowarray (Rec rec[]) { -          for(inti = 0; i < rec.length; i++){ -              theSystem.out.print (Rec[i].key + ""); -         } -     } -      PublicRec[] Getrecarray (intarray[]) { +Rec result[] =NewRec[array.length]; -          for(inti = 0; i < Array.Length; i++){ +Result[i] =NewRec (Array[i]); A         } at         returnresult; -     } -}

Test.java

1  PackageCom.gxf.sort;2 3  Public classTest {4 5      Public Static voidMain (string[] args) {6REC REC =NewRec ();7QuickSort QuickSort =NewQuickSort ();8         9         intArray[] =New int[]{0, 32, 1, 34, 54, 5, 6};TenRec array_rec[] =Rec.getrecarray (array); OneSystem.out.println ("Order before using Quick sort:"); A Rec.showarray (ARRAY_REC); -          - Quicksort.sort (ARRAY_REC); the          -SYSTEM.OUT.PRINTLN ("Use order after quick sort:"); - Rec.showarray (ARRAY_REC); -     } +  -}

PS: Here is the main reference to the data structure of the West version

Quick Sort Quicksort

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.