My Java Development Learning journey------A quick sort of >java classic sorting algorithm

Source: Internet
Author: User
Tags benchmark

first, the algorithm thought
The quick sort is a sort of division exchange proposed by C.r.a.hoare in 1962. It adopts a strategy of division, which is usually referred to as the Division method (Divide-and-conquermethod).


(1) The basic idea of division and Administration Law
The basic idea of divide-and-conquer method is: to decompose the original problem into several sub-problems with smaller size but similar structure to the original problem. Solve these sub-problems recursively, then combine the solutions of these sub-problems into the solution of the original problem.


(2) The basic idea of fast sequencing
Set the current unordered area to be sorted as R[low. High], the basic idea of fast sequencing can be described by using the divide-and-conquer method:
① decomposition:
In R[low. High] To select a record as the Datum (Pivot), which divides the current unordered division into left and right two smaller sub-intervals r[low. PIVOTPOS-1) and R[pivotpos+1..high], and make all recorded keywords in the left sub-range less than or equal to the datum record (may be remembered as pivot) Keyword Pivot.key, all recorded keywords in the right sub-range are greater than or equal to Pivot.key, while Datum record pivot is in the correct position
(Pivotpos), it does not need to participate in subsequent sorting.
Attention:
The key to partitioning is to require the location of the benchmark record Pivotpos. The results of the partitioning can be simply expressed as (note Pivot=r[pivotpos]):
R[low. Pivotpos-1].keys≤r[pivotpos].key≤r[pivotpos+1..high].keys
which Low≤pivotpos≤high.


② Solution:

Quick Sort by recursive call to left and right sub-interval r[low ... PIVOTPOS-1] and R[pivotpos+1..high] quick sort.


③ combination:

since the two recursive calls in the solve step end, their left and right two sub-bands are ordered. For a quick sort, the combination step does not have to do anything and can be seen as an empty operation.


Second, fast sorting algorithm quicksort

  void QuickSort (Seqlist r,int low,int High)   {//to R[low: High] fast sort     int pivotpos;//The position of the base record after division     if (low


Note:
To sort the entire file, you only need to call Quicksort (R,1,n) to complete the r[l. N] of the sort.


Third, the division algorithm partition
(1) Simple partitioning method

① Concrete Practices
First step: (initialize) set two pointers I and J, their initial values are the lower bound and upper bounds of the interval, namely I=low,i=high; Select the first record of the unordered area R[i] (i.e. R[low]) as the Datum record, and save it in the variable pivot;
The second step: to scan the J from high to the left until the 1th keyword is found to be less than Pivot.key's record r[j], the r[j]) moved to the position I referred to, which is equivalent to R[j] and Datum r[i] (that is, pivot) is exchanged, Moves the record with the keyword less than the base keyword Pivot.key to the left of the datum, the equivalent of pivot in the interchange R[j], and then the I pointer scans to the right from the i+1 position until it finds a record with a 1th keyword greater than Pivot.key r[i], R[i] Move to the position I referred to, which is equivalent to exchanging r[i] and datum r[j], so that the key is greater than the base keyword record to the right of the datum, after the Exchange R[i] is the equivalent of holding pivot, and then the pointer J from the position j-1 start to the left to scan, so alternately change the scanning direction, From each side to the middle of each other, until i=j, I is the final position of the Datum pivot, the pivot is placed in this position to complete a division.


② One Division Process
In the process of one division, the specific changes "See Animation Demo"

(  http://student.zjzk.cn/course_ware/data_structure/web/flashhtml/kuaisupaixu.htm)

③ partitioning algorithm:
 

int Partition (seqlist r,int i,int j)    {//Call Partition (R,low,high) for R[low:        while (I<j&&r[j].key>=pivot.key)//pivot is equivalent to j--on position I          ,//right-to-left scan, find 1th keyword less than pivot.key record R[j]        if (i<j)//Indicates the keyword found for r[j] <pivot.key            R[I++]=R[J];///equivalent to Exchange R[i] and R[j], after exchange I pointer plus 1        while (I<j&&r[i].key<=pivot.key)//pivot equivalent to on position J            i++ ;//left-to-right scan, look for a 1th keyword greater than Pivot.key records R[i]        if (i<j)//means found r[i], so R[i].key>pivot.key            r[j--]=r[i]; Equivalent to Exchange R[i] and R[j], after Exchange J pointer minus 1       }//endwhile      R[i]=pivot;//Datum record has been last positioned      return i;    }//partition


Iv. Quick Sort Execution process
the whole process of quick sort execution can be described by a recursive tree.





Analysis:
(1) The route with the arrows in the recursive execution is shown in the envelope line.
(2) The left side square brackets on each node in the recursive tree indicate the current interval to be sorted, and the keywords within the nodes are the base keywords for the division.
Attention:
The sub-intervals corresponding to the leaf nodes have only one keyword, which does not need to be divided, so there are no datum keywords in the leaf node.
(3) The left and right two sub-intervals are marked in the left square brackets of the left and right two children nodes of the node respectively.
The "example" root node left side square bracket [49,38,65,97,76,13,27,49] denotes the initial keyword to be sorted, and 49 in the root represents the keyword of the selected baseline record, dividing the result [27,28,13]49[76,97,65,49_], The left and right sub-zones are labeled at the root node of the two children on the other side.
(4) The contents of the parenthesis in the right side of each branch node represent the results returned after the sorting process for the left side of the node is finished. It is the right and left child corresponding to the interval after the completion of the order, the left and right children of the corresponding sorting results are placed in the branch node before and after the keyword sequence.
"Example" Branch node 76 of the children of the corresponding interval ordered by the results are (49_,65) and (97), respectively, placed in the 76 before and after (49,65,76,97), which is the node 76 left side interval [76,97,,65,49] The result of sorting.
(5) The order of execution of the algorithm is the order of the arrows in the recursive tree, in fact, when the partitioning operation is regarded as an operation to access the node, the execution of the fast sort is equivalent to the first order traversal of its recursive tree.
Attention:
Any recursive algorithm can use recursive tree to describe its execution process.


The change of state after the division of the Fast sorting
[49 38 65 97 76 13 27 49]//Initial keywords
[27 38 13] 49 [76 97 65 49]//1th division completed, corresponding to the recursive tree 2nd layer
[13] 27 [38] 49 [49 65] 76 [97]//To the previous level of the various unordered zones, corresponding to the 3rd layer of recursive tree
13 27 38 49 49 [65] 76 97//To the previous level after each unordered partition is completed, corresponding to the 4th layer of recursive tree
13 27 38 49 49 65 76 97//FINAL sort result




Six, algorithm analysis
The time of fast sorting is mainly spent on the division operation, the interval of length k is divided, the total need k-1 the comparison of the key words.


(1) Worst time complexity
The worst case is that each time the selected datum is the smallest (or largest) record of the keyword in the current unordered region, the rowThe result of the division is that the sub-interval to the left of the Datum is empty (or the right sub-interval is empty), and another non-empty sub-The number of records in the interval is only one less than the number of records in the unordered area before partitioning.
Therefore, a fast sort must be n-1, and the interval length at the beginning of the first Division is n-i+1, and the required comparisonThe number of times is n-i (1≤i≤n-1), so the total number of comparisons reached the maximum value:
Cmax = N (n-1)/2=o (n2)
If you follow the partitioning algorithm given above, each time you take the 1th record in the current unordered area as the benchmark, then when the file'swhen a record has been arranged in ascending order (or descending order), the base of each partition is the smallest of the keywords in the current unordered region (ormaximum), the number of comparisons required to quickly sort is the most.


(2) Best time complexity
in the best case, each time the baseline is taken is the "median" record of the current unordered area, and the result of the partition isthe left and right two unordered sub-bands of the datum are roughly equal in length. Total number of keyword comparisons:
0 (NLGN)
Note:
It is easier to use recursive trees to analyze the best case comparisons. Because the length of the left and right sub-intervals after each partitionroughly equal, so the height of the recursive tree is O (LGN), and the partitioning process of each node in the recursive tree is requiredthe sum of the number of keyword comparisons is not more than N, so the total number of keyword comparisons required for the entire sort process C (n) =o(NLGN).
The worst time complexity for fast sorting, because the number of records moved quickly is not greater than the number of comparisonsshould be 0 (N2), preferably with a time complexity of O (NLGN).


(3) Selection of benchmark keywords
Selecting the Divided datum keywords in the current unordered area is the key to determining the performance of the algorithm.
① The rules of "three taking in"  
The "three take in" rule, that is, in the current interval, the first, the end of the interval and the middle position of the keyword comparison,The records corresponding to the values of the three are used as a benchmark, and the baseline record and the 1th record in the area are recorded before the division begins.The partitioning process is exactly the same as the partition algorithm given above.
② random number K (Low≤k≤high) between low and high, using r[k] as the benchmark  
the best way to select a datum is to use a random function to generate a random number between low and high K(Low≤k≤high), using r[k] as the benchmark, which is equivalent to forcing the R[low. The records in high] are randomly distributed. The quick sort obtained by this method is generally called a random quick sort.
Note:
The fast ordering of randomization is very different from the general fast sorting algorithm. But after randomization, the performance of the algorithm is greatly, especially in the case of initially ordered documents, is generally not likely to lead to the worst of events. The randomization of the algorithm does notit is only suitable for fast sequencing, but also for other algorithms that require random distribution of data.


(4) Average time complexity
Although the worst time for fast sorting is O (N2), in terms of average performance, it is based on the internal of the keyword comparisonThe fastest in the sorting algorithm, and hence the name of the fast sort. Its average time complexity is O (NLGN).


(5) Complexity of space
Quick sort requires a stack within the system to implement recursion. If each partition is more homogeneous, its recursive treeThe height is O (LGN), so the stack space after recursion is O (LGN). In the worst case, the height of the recursive tree is O (n), the desiredThe stack space is O (n).


(6) Stability
Fast sequencing is non-stable, such as [2,2,1].



VII. implementation of code

/** * A quick sorting algorithm is: 1), set two variables I, J, i:=1,j:=n at the beginning of the sorting, 2) with the first array element as the key data, assigned to X, that is, X:=a[1], 3), starting from J forward search, that is, from the beginning of the forward search (j:=j-1), Find the first value less than X, Exchange the two; 4), search backwards from I, that is, start backward search (i:=i+1), find the first value greater than X, Exchange 5), repeat steps 3rd and 4 until i=j; */public class QuickSort {public static void Main (string[] args) {int[] Source = {49, 38, 65, 97, 76, 13, 27}; System.out.print ("Initial keyword:");p Rintarray (source); System.out.println (""); QuickSort (source, 0, source.length-1); System.out.print ("\ n-sort result:");p Rintarray (source);} /* * First write the algorithm according to the array for the data prototype, and then write the extensibility algorithm. Array {49,38,65,97,76,13,27} */public static void QuickSort (int[] source, int low, int. high) {int pivotpos;//the position of the Datum record after division if (l ow < high) {Pivotpos = partition (source, low, high);//The source array is divided quicksort (source, low, pivotPos-1);//left interval recursive sort quick Sort (source, Pivotpos + 1, high);//}}public static int partition (int[] source, int low, int.) {int pivot = so  urce[low];//uses the 1th record of the interval as the datum while (Low < high) {//from the interval to the middle, until Low=high (Low < high && Source[high] >= pivot) {//High find larger than povite, then meet the requirements, continue to look for high--;//right-to-left scan, find the 1th keyword less than pivot record source[j]}if (Low < High) {//= find Source[j] Keyword < pivotsource[low++] = source[high];//equivalent to interchange source[i] and Source[j], after Exchange I plus 1}system.out.print ("right-to-left scan <----" + "This trip sort result:") ;p Rintarray (source), while (Low < high && Source[low] <= pivot) {//Lows start finding smaller than povite, meet requirements, continue looking for low++;//left-to-right scan , find the 1th keyword greater than Pivot's record source[i]}if (Low



Viii. Results of operation

Initial keyword: 49386597761327 right-to-left scan <----  Sort results: 27386597761327 scan from left to right---->  sort Result: 27386597761365 right-to-left scanning < ----  This order results: 27381397761365 scan from left to right---->  sorted results: 27381397769765 right-to-left scanning <----  Sort results: 27381397769765 scan from left to right---->  sorted results: 27381397769765pivot=49 This is a sort of result: 27381349769765 right-to-          left scanning <----  Sort results: 13381349769765 scan from left to right---->  sort results: 13383849769765pivot=27 This is a sort of result:          13273849769765 scan from right to left <----  Sort results: 13273849659765 scan from left to right---->  sort result: 13273849659797pivot=76          Sort results: 13273849657697 sorted Results: 13273849657697

==================================================================================================

Ouyangpeng welcome reprint, sharing with people is the source of progress!

Reprint please keep the original address : Http://blog.csdn.net/ouyang_peng

==================================================================================================



My Java Development Learning journey------A quick sort of >java classic sorting algorithm

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.