Quick ordering of C + + code for the algorithm

Source: Internet
Author: User

Quick sort, short-line, often called quicksort, QSort. It is very common in the sorting algorithm, its programming complexity is low, the time complexity O (LOGN), the Space Complexity O (N), the execution efficiency is stable, and the constant is very low.

The basic idea is two points, for example you want to sort N the number, you call the Qsort (1,n). So the quick line will do this:

1. Find a number X

2, the n number is divided into two parts, the left is smaller than X, the right is bigger than X

3. Call Qsort on both sides respectively

Obviously, this is a two-point, recursive implementation.

First, the second step, the code is not too ugly, the complexity of the time is O (N), sweep over it. So, the point is the first step--we assume that you are looking for x time is O (1), then if your x each find is the median, then the algorithm time is O (nlogn), if your x every time you find the number of the edge (so that you divide the n number into 1 and N-1), then the algorithm time is O (N²). Therefore, only in the excellent choice of x method, the fast platoon can guarantee the complexity of O (Nlogn).

Let's discuss the first step in detail (the actual meaning of the "median" is analyzed below, and two common implementations are given, and the master can skip directly to the code).

  We ideally want to find the median, but you can't really find the median, because that time is O (N). The novice is very headache, "This is the whole?" "The method is simple : just pick one."

  Novice more headache ... "you choose a random, the timing of course is O (1), but you can guarantee the complexity of the algorithm is not degraded? "In fact, I can not guarantee that the algorithm does not degenerate, but I know from the probability that I randomly selected every time, most of the time did not degenerate much, the result is only very low and very low probability degenerate into O (N²). Novice very despise, "if I can give you to construct a set of data, let you choose the edge every time ah!" Does it degenerate into O (N²)? "This is actually impossible." Because I am not a fixed number of positions, but a random selection, so you simply can not construct, how much I degenerate, only depends on the probability.

  Novice to give up ... "You this fast platoon complexity directly depends on the probability, but I did not learn, I do not know the speed of the degenerate probability is how much, how dare I use Ah! What if I just degenerate when I use it? "This is what I want to focus on today and the vast number of novices, you have come into contact with a very important concept in an algorithm: stochastic algorithm .

  Algorithm complexity, just a very coarse description of the algorithm. You know the complexity of an algorithm is O (N²), in fact, you just know that it is a two-stage only, do not know the real complexity. What is the constant of complexity? Is it 3n², 0.3n², or 1.3n²? Usually we do not analyze, because we are in accordance with the maximum complexity analysis. Title to you n=1000, you know the algorithm complexity O (N²), but also know that the constant is very large (for example, 100) The program can run after less than 1s, so you dare to write. But now, the algorithm is random, good time O (Nlogn) constant is very small, when the Bad O (N²) constant is very large, you dare not analyze?

  Some people may not dare to use, think as long as the probability can not guarantee that no problem, in case the test met on the miserable. This kind of thought is generally novice only then will have, please must convince oneself! My reason is very simple, the probability is too high I do not dare to use, my practice is, the probability of dropping to a certain day than you go out of the flower pot accidentally killed by the probability is still low, I dare to use, because I am sure I will not go out one day by the flowerpot killed .

  Of course, this is clear, and the question now is, what if we don't analyze it? In the long run, you should go back and study the probability, and then come back to the good analysis, in the short term, there is no easy way? Of course, it's a test. You randomly out a lot of data, with your written fast line to test, found that their worst can also be counted as O (Nlogn), that basically no problem, because the actual test and actual application data is basically the case.

Well, said so much, in fact, just because understanding the idea of the fast line is a threshold for many beginners. I hope to be able to say more nonsense, to help a lot of beginners to the smooth pass. In this way, many of the Novice's future algorithm is beneficial to the path of harmless. Let's discuss how the first step should actually be done:

It must be clear that if the selection method is too complex, the algorithm constants will become larger, and if the method is too simple, then the complexity of the algorithm will degenerate. Therefore, comprehensive consideration, analysis and a large number of measurements, the more common good writing and quick to write two kinds. Suppose you have N number A[1~n]:

1, X=mid (a[1],a[(1+n)/2],a[n]), mid refers to the median of these three numbers. This is the most common method, if I remember correctly, this algorithm is also the C + + algorithm library (algorithm) inside the wording. The actual situation shows that this kind of extraction efficiency is very high.

2, X=a[randint (1,n)], that is, the subscript to take the 1~n in a random number. This is also a more commonly used method, the benefit is the real guarantee of randomness, but the disadvantage is that the generation of random number time is relatively high, will cause the algorithm constant to become larger.

Speaking so much, the novice may feel that I still do not explain why the complexity of the fast line is O (Nlogn). I can only say that in order to analyze the complexity of the fast-running requires a very fine analysis and a lot of data, I will have the opportunity to write an article to analyze, now I can only tell you from the probability that most of the time is O (Nlogn), and the fast-row constant than the heap is a lot smaller (time is about one times faster, You may not be able to meet the data of the fast row. I dare not say that no data can be card fast row, but I can be sure, if not specifically to card you, so write fast row certainly no problem, anyway I dare to use the exam. If someone is stuck with you, you can write a pile of rows, a constant, but it's impossible to get stuck.

Here's My Code:

1InlinevoidSwapint&a,int&AMP;B) {intT=a; A=b; b=t;}2 3InlineintMidintAintBintc)4 {5     if(a>b) Swap ( A, a);6     if(b>c) Swap (B,C);7     if(a>b) Swap ( A, a);8     returnb;9 }Ten  One voidQSort (intA[],intLintR// ?????? A { -     if(L&GT;=R)return; -     intI=l,j=r,x=mid (a[l],a[(l+r) >>1],a[r]); the      while(true) -     { -          while(a[i]<x) + +i; -          while(a[j]>x)--J; +         if(I&GT;J) Break; -Swap (a[i],a[j]); ++i; --J; +     } A QSort (A,L,J); QSort (a,i,r); at}

Quick ordering of C + + code for the 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.