A summary of the time complexity of the algorithm introduction learning and various sorting algorithms

Source: Internet
Author: User

A fast queue is the most commonly used sorting algorithm, because its average time complexity is NLGN, and the constant factor is relatively small.

I. Quick sort
Both the fast and the merge sorts are based on the sorting algorithm of divide-and-conquer; The division of the fast line is as follows:
Decomposition: To the interval a[p,r] decomposition, return q, so that a[p–q-1] is not greater than a[q] a[q+1,r] are more than a[q];
Solution: The resulting interval continues to be recursively scheduled
Merge: Because the Clippers are sorted in situ, there is no need for a special merge
As can be seen from the most important is the decomposition function, which divides the array into 3 parts according to the key values, the implementation of the process is described in code comments.

We generally take the last element of the array as the key value for the comparison, as in the following code

intParatition (int*a,intPintR) {///   at loop time A[p--i] represents an element that is less than key  ///  A[i+1--j] Represents an element that is currently greater than key    ///The process of dividing each element by comparing it to these two intervals (mainly I growth).     ///Of course Finally, a[i+1] and A[r] are exchanged, so that a[i+1] represents the partitioning element    intKEY=A[R];///   take the last element as the key value of the comparison    inti=p-1; for(intj=p;j<r;j++)if(A[j]<=key)            {i++;        SWQ (A[j],a[i]); } SWQ (a[i+1],a[r]);returni+1;}

But we can also take an element in the interval a[p,r] as the key value, so that each partition more evenly, thus improving efficiency, the corresponding code is as follows:

///随机划分函数int RandParatition(int *a,int p,int r){    int x=rand()%(r-p+1)+p;   ///产生一个[p,r]之间的随机数x    swq(a[x],a[r]);  ///将a[x],a[r]交换,使得将a[x]作为划分的关键值    return Paratition(a,p,r);}

Here is a complete code for the quick line:

#include <iostream>#include <cstdio>#include <ctime>#include <cstring>usingnamespace Std;voidSWQ (int&a,int&AMP;B) {intT=a;    A=b; b=t;}///   Partitioning functionintParatition (int*a,intPintR) {intKEY=A[R];inti=p-1; for(intj=p;j<r;j++)if(A[j]<=key)            {i++;        SWQ (A[j],a[i]); } SWQ (a[i+1],a[r]);returni+1;}//   Quick-linevoidQiucksort (int*a,intPintR) {if(P&GT;=R)return;intQ=paratition (A,P,R); Qiucksort (a,p,q-1); Qiucksort (a,q+1, r);}///   above is a general quick row, there is a random quick row////   The idea of a random quick line is to divide a[p,r] when we don't always choose a[r] as the key value////   instead of taking an element at random in A[p,r] as the key value for partitioning. ///   random partitioning functionintRandparatition (int*a,intPintR) {intX=rand ()% (r-p+1) +p;///   generates a random number between [p,r] xSWQ (A[x],a[r]);///   will a[x],a[r] exchange, making a[x] The key value of the Division    returnParatition (a,p,r);}///   random quick-line functionvoidRandqiucksort (int*a,intPintR) {if(P&GT;=R)return;intQ=randparatition (A,P,R);///random   fast row call is random partition functionRandparatition (a,p,q-1); Randparatition (a,q+1, r);}intMain () {intn=5, a[Ten]; cout<<"Please enter"<<n<<"Number:"<<endl; for(intI=1; i<=n;i++) cin>>a[i]; ///Randqiucksort (a,1,n);Qiucksort (A,1, n); cout<<"sorted array after:"<<endl; for(intI=1; i<=n;i++) cout<<a[i]<<" "; cout<<endl;return 0;}

Two. The time complexity of the fast line: The time complexity analysis of the fast row is a very troublesome thing, we know that if each division is completely unbalanced namely T (N) =t (n-1) +o (n), then the time complexity of the fast row is n^2, if each time is equal division that is T (n) =t (N/2) +t (n /2) +o (n), then the complexity of time is NLGN. However, these two divisions are not common in peacetime, so they are not representative; But if we examine some of the imbalances, such as 9:1, T (n) =t (N/10) +t (N*9/10) +o (n), we can see that in this case the time complexity is still NLGN , and even if the imbalance is further increased (as long as it is not completely unbalanced), to a degree of 99:1, the time complexity will still be NLGN (a large proof of the introduction of this algorithm). So we can say that the average time complexity of the NLGN is the same, and this complexity is often achievable.

Three. Summary of time complexity of common sorting algorithms
The following is a simple list of the time complexity of the sorting algorithm previously studied, and does not give a strict proof.

 排序算法          最好时间复杂度               最坏时间复杂度 插入排序         O(n)(原数组有序)            O(n^2)(原数组逆序) 合并排序         O(nlgn)                    O(nlgn) 堆排序           O(nlgn)                    O(nlgn) 快速排序         O(n^2)(每次都是完全不平衡划分) O(nlgn)(大多数情况)

From the table above, it seems that the combination of sorting and heap sorting is better than the fast, but in fact, because the time complexity of the Fast row O (NLGN) is often achievable, and the fast row in the constant factor is relatively small, so in the actual fast platoon is generally the fastest sorting algorithm, worthy of his "quick Platoon" title.

A summary of the time complexity of the algorithm introduction learning and various sorting algorithms

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.