The path for algorithms to regain -- fast sorting

Source: Internet
Author: User

The path for algorithms to regain -- fast sorting

 

 

Chapter 1: divide and Recursion

 

Quick sorting

 

Algorithm Description:

Just like merging and sorting, a batch of data is sorted.

The principle of quick sort is to find a standard and scale it around. Just like when I was doing exercises, the teacher would say that, on the basis of XXX, the gymnastics formation was dispersed. Then, people around XXX would look at it and spread it out. The principle of fast sorting is similar. The most primitive is to take the first element as the standard, and then the element is smaller than the element on the left and larger on the right.

Note that the first element here is the leftmost element of the input parameter, not the first element of the entire array.

In general, there are three steps:

<1> Divide (decomposition): an element a [m] in the array is used as the benchmark. The entire array a [0: n-1] is divided into three parts: all elements in the array less than a [m] a [0 M-1], a [m], a [m + 1, n-1] Set of all elements greater than a [m] in the array.

<2> Conquer (recursive solving): uses recursive calling to sort a [M-1] And a [m + 1, r] respectively.

<3> Merge (Merge): Because sorting a [M-1] And a [m + 1, r] is performed locally, therefore, the merge is completed without executing any other computation.

 

Program code:

 

Template
 
  
Void Swap (Type & a, Type & B) {Type temp = a; a = B; B = temp;} template
  
   
Int Partition (Type arr [], int l, int r) {// The initial data is like this. For example, arr has 6 numbers, l is 0, r is 5, // The following table of the array is 0 ~ Position 5 (6 elements in total) for sorting int I = l, j = r + 1; Type temp = arr [l]; while (true) {// search from the past, less than the number subscript of the first element (temp) while (arr [++ I] <temp & I <r ); // search from the back and forward. It is greater than the number subscript of the first element (temp) while (arr [-- j]> temp); if (I> = j) break; // switch the number of two subscript elements to Swap (arr [I], arr [j]);} // Finally, place the number of the first element in the middle arr [l] = arr [j]; arr [j] = temp; return j;} template
   
    
Void QuickSort (Type arr [], int l, int r) {if (l <r) {int m = Partition (arr, l, r); QuickSort (arr, l, m-1); QuickSort (arr, m + 1, r );}}
   
  
 


 

Algorithm analysis:

The running time of the fast sort is related to the symmetry of the Division. The worst case occurs when the two regions in the division process contain 1 and n-1 elements respectively. Because the calculation time of the Partition function is O (n), assuming that the division of each step of the Partition function is the most asymmetric division, the complexity of the calculation time T (n) is:

T (n) = O (1) WHEN n ≤ 1

= T (n-1) + O (n) WHEN n> 1

By solving this recursive equation, T (n) = O (n ^ 2) can be obtained)

However, in the best case, every division is equal, then the computing time complexity T (n) is:

T (n) = O (1) WHEN n ≤ 1

= 2 * T (n/2) + O (n) WHEN n> 1

By solving this recursive equation, T (n) = O (nlog (n) can be obtained ))

Furthermore, it has been proved that the average complexity is also O (nlog (n), which is fast in the comparison-based sorting algorithm. Therefore, it is named "quick sorting.

 

Algorithm Optimization:

The optimization of quick sorting is the optimization of benchmark. In algorithm analysis, we can also see that a good benchmark is very important, so its optimization lies in this.

In the above functions, a random is used to obtain a subscript position, instead of the value of the first element.

 

 

 

 

* *********************************** Please specify the source: bytes ********************************************

 

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.