Introduction to Algorithms-chapter 7 high-speed sequencing

Source: Internet
Author: User
Tags virtual environment

Order

High-speed sequencing (QuickSort) is also a sort algorithm for an input array that includes n arrays. The worst-case execution time is O (n^2).

Although the worst-case execution time is worse. But high-speed sorting is generally the best useful choice for sorting. This is because its average performance is quite good. The expected execution time is O (NLGN). and the implied constant factor in O (NLGN) is very small. It also enables in-place sequencing to work very well in a virtual environment.


GITHUB Chapter 7 program code download

Principle

The high-speed sort is also the same as the combined sort, which is divided into three steps: decomposition, resolution and merging.
Decomposition: Array Array[low...high] is divided into two (possibly empty) sub-arrays array[low...temp-1] and Array[temp+1...high]. So that each element in array[low...temp-1] is less than or equal to array[temp], and each element in Array[temp+1...high] is greater than array[temp], the subscript temp is also computed in this process;
Workaround: High-speed sequencing by recursive invocation. Sub-array Array[low...temp-1],array[temp+1...high] to sort;
Merge: Because two sub-arrays are sorted in place. The entire array Array[low...high] is already sequenced, without the need to manipulate their merge.

This chapter introduces the principle of high-speed sequencing algorithm, program implementation (including the random version number) and its performance analysis.

Fast algorithm implementation
#include <iostream>#include <ctime>#include <cstdlib>#define Nusing namespace STD;//recursive algorithm for high-speed sequencingvoidQuickSort (int*Array,intLowintHigh);//Seek cutting pointintPartitionint*Array,intLowintHigh);//Exchange values for two variablesvoidExchangeint&a,int&AMP;B);intMain () {//Declare an array to be sorted    int Array[N];//Set randomization seed to avoid generating the same random number each timeSrand (Time (0)); for(inti =0; i<n; i++) {Array[I] = rand ()%101;//array assignment uses random functions to generate random numbers between 1-100}cout<<"before sorting:"<< Endl; for(intj =0; j<n; J + +) {cout<<Array[j] <<"  "; }cout<< Endl <<"After sorting:"<< Endl;//Call the high-speed sort function to sort the arrayQuickSort (Array,0N1); for(intK =0; k<n; k++) {cout<<Array[K] <<"  "; }cout<< Endl;return 0;}//mainvoidQuickSort (int*Array,intLowintHigh) {if(Low < High) {intTEMP = partition (Array, low, high); QuickSort (Array, Low, temp-1); QuickSort (Array, temp +1, high); }}intPartitionint*Array,intLowintHigh) {inti = low-1;//default will divide the last element of the segment as the main element    intx =Array[High]; for(intj = Low; jif(Array[j] <= X)//On array[i] The left side is less than x is the number of Array[high], the right is greater than its number{i + =1; ExchangeArray[I],Array[j]); }} Exchange (Array[i +1],Array[High]);returni +1;//So after the loop is complete. I+1 is the cutting point of the array.}voidExchangeint&a,int&AMP;B) {inttemp = A;    A = b; b = temp;}
Random version number for high-speed sequencing

In the implementation of the high-speed sequencing algorithm described above, Partition (A, p, R) is always the default A[r] as a comparison standard. Assuming that random sampling techniques can be used. will make the analysis easier. The following is a high-speed sorting algorithm implementation of the randomization version number:

#include <iostream>#include <ctime>#include <cstdlib>#define Nusing namespace STD;//recursive algorithm for high-speed sequencingvoidQuickSort (int*Array,intLowintHigh);//Seek cutting pointintPartitionint*Array,intLowintHigh);//A random element between low and high as the main element, to find the cutting pointintRandompartition (int*Array,intLowintHigh);//Exchange values for two variablesvoidExchangeint&a,int&AMP;B);intMain () {//Declare an array to be sorted    int Array[N];//Set randomization seed to avoid generating the same random number each timeSrand (Time (0)); for(inti =0; i<n; i++) {Array[I] = rand ()%101;//array assignment uses random functions to generate random numbers between 1-100}cout<<"before sorting:"<< Endl; for(intj =0; j<n; J + +) {cout<<Array[j] <<"  "; }cout<< Endl <<"After sorting:"<< Endl;//Call the high-speed sort function to sort the arrayQuickSort (Array,0N1); for(intK =0; k<n; k++) {cout<<Array[K] <<"  "; }cout<< Endl; System"Pause");return 0;}//mainvoidQuickSort (int*Array,intLowintHigh) {if(Low < High) {inttemp = Randompartition (Array, low, high); QuickSort (Array, Low, temp-1); QuickSort (Array, temp +1, high); }}intPartitionint*Array,intLowintHigh) {inti = low-1;//default will divide the last element of the segment as the main element    intx =Array[High]; for(intj = Low; jif(Array[j] <= X)//On array[i] The left side is less than x is the number of Array[high]. The right side is greater than its number{i + =1; ExchangeArray[I],Array[j]); }} Exchange (Array[i +1],Array[High]);returni +1;//So after the loop is complete, i+1 is the cutting point of the array}intRandompartition (int*Array,intLowintHigh) {//Find a random position between low and high    inti = rand ()% (high-low +1) + low;//Swap the random principal to the tail,ExchangeArray[I],Array[High]);returnPartitionArray, low, high);}voidExchangeint&a,int&AMP;B) {inttemp = A;    A = b; b = temp;}

The random version number of the fast and ordinary fast row difference is not very large, modified but to find the main element in the cut point step selection, that is, the addition of the Randompartition function, the selection of the main element subscript I. The element is swapped to the end of the segment, and the partition function is still called to seek the cutting point.

High-speed Sequencing performance analysis

The execution time of high-speed sequencing is related to the symmetry of partitioning. The latter is related to the choice of which element to divide. Assuming the partitioning is symmetric, the algorithm is as fast as the merge sort in the asymptotic sense. Assuming that the partitioning is incorrect, then the algorithm is as slow as the insertion sort in a gradual sense. The following sections discuss the worst case division, the best case Division, and the Division of balance for high-speed sequencing respectively.
worst case Partitioning: The worst case partitioning behavior of high-speed sequencing occurs when the two regions that are generated during the partitioning process include n-1 elements and 0 elements respectively. It is assumed that each recursive invocation of the algorithm has such an incorrect name division. The time cost for partitioning is O (n). Since t (n) =o (1) is returned after recursive invocation of an array of size 0, the execution time of the algorithm can be recursively expressed as:
T (n) = t (n-1) + t (0) + O (n) = t (n-1) + O (n)
From the intuitive point of view. Assuming that the cost of each layer of recursion is added together, it is possible to obtain a arithmetic progression (equation (array,2) with a value of very O (n^2)) using the substitution method to prove that the solution of the recursive t (n) = t (n-1) + O (n) is t (n) = O (n^2).
So assuming that at each level of the algorithm recursion, the division is the most incorrect. Then the execution time of the algorithm is O (n^2), that is, the worst-case execution time of the high-speed sorting algorithm is better than the insertion sort.

In addition, when the input array is fully sequenced, the execution time of the high-speed sort is O (n^2), and the execution time of the insertion sort is O (n).
Best Case Partitioning: in the most balanced partitioning possible for partition, the size of the resulting two sub-problems is unlikely to be greater than [N/2], since the size of one of the sub-problems is [N/2]. Then the size of the other sub-problem must be [n/2]-1]. In such a case. High-speed sequencing is performed much faster. The recursive expression of its execution time is:
T (n) <= 2T (N/2) + O (n)
The recursive formula can be solved by T (n) = O (NLGN). Since both sides of the recursive division of each layer are symmetrical. So from a progressive point of view. The algorithm executes much faster.


Division of Balance: The average execution time of high-speed sequencing is very close to the execution time of its best case, rather than very close to its worst-case execution time (proof of Reason specific reference to "Introduction to the algorithm" the second edition of the original book P88), because no matter what kind of a constant proportion of the Division will produce a depth of O (LGN) A recursive tree in which each layer is at the cost of O (n), so that the total execution time is O (NLGN) whenever the partition is divided according to the constant proportions.

Introduction to Algorithms-chapter 7 high-speed sequencing

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.