[] Several common sorting 2 (qsort)

Source: Internet
Author: User

* ************************* For reprinting, please indicate the source! ******************************

Qsort is a sorting algorithm, as its name implies, and it is fast, right. So where should we start? After writing this, I found that when talking about the previous algorithms, I did not mention the validity period. 0.0 proved that Hao is indeed a very bad programmer. Insert, select, bubble, bla ~ We regard them as O (N ^ 2) algorithms. Here the O is a progressive sign, indicating the progressive upper bound. Of course, there is also a token, it also progressively gives the upper and lower bounds of the function. The rigorous judgment method should of course refer to the number of algorithm running steps as a function related to input scale N. Of course, accurate computation seems very troublesome. When n → ∞, many items can be ignored. If we ignore the coefficient, we will get the above O () (it seems like this is the case. This is why I didn't say this in the overview, and I am not clear about it myself ). Qsort is much more efficient, with an average O (nlogn ). The worst case is O (n ^ 2 ). Now let's go to the question: qsort!

Basic description

First of all, fast sorting is based on the idea of sub-governance. It is divided into several steps. First, we need to sort the Array (first, we need to find a margin (I don't know if it is swollen, but it is used as a comparison standard ), move a part smaller than the primary node to the other side of the primary node ). Then, recursively use qsort to sort the arrays on both sides of the sort. The boundary can be set to a number or two numbers. It can also be set to insert sorting when the number is smaller than a certain number. They have a relatively small overhead and are not inferior to the fast sorting when the number is small.

Implementation

The split step is called partition (A, P, R). It returns the coordinates of the shard. Assuming that we have completed this function, let's take a look at how the Quick Sort completes the work. The pseudocode is as follows:

Quicksort (A, P, R)

If P <r

Then Q partition (A, P, R)

Quicksort (A, P, q-1)

Quicksort (A, q + 1, R)

It looks concise, right? Bingo ~ Qsort was not very difficult, but when many people started learning it, they didn't have a good idea of recursion and Governance Division. After receiving it, it was taken for granted ~ Take a closer look. The above pseudo code does not deal with the boundary in particular, and the pointer is crossed. During work (P is the left boundary, r is the boundary), after division, Q gets the vertex position. In this case, the number of arrays on the left of Q is smaller than that on the left of Q, and the number on the Right of Q is greater than that on the right of Q (from small to large ). After sorting P to the r array, it is successfully decomposed into sorting P to the Q-1 array and sorting q + 1 to the r array. The array length is getting shorter and shorter. When p = R, Q
= P = R. Then it stops when you call quicksort. The proof that the cycle remains unchanged will not be copied. I cannot write such a rigorous proof myself. It proves in detail that you can check the information such as Google.

 

The next step is the key part of the algorithm. Partition! The pseudocode is as follows:

Partition1 (A, P, R)

X branch a [R]

I'm P-1

For J branch P to R-1

Do if a [J] <= x

Then I have I + 1

Exchange a [I] a [J]

Exchange a [I + 1] a [R]

Return I + 1

Note that this is partition1, which is not commonly used. However, since we will introduce it in introduction, let me put it in front. Two variables, x I. X is used as the dividing line, while I is the dividing line, which is scanned from left to right. When working, the left side of J is a part of the split, where the number on the left side of I is less than X, when J sweeps to the R-1, the left side of I is less than X, therefore, after switching a [I + 1] and a [R], the Division is completed. Similarly, this is not a strictly cyclical proof, but there is a general frame to illustrate the correctness of this division.

Partition2 (A, P, R)

X branch a [p]

I'm P-1

J branch R + 1

Whie true

Do repeat J 1_j-1

Until a [J] <= x

Repeat I found I + 1

Until a [I]> = x

If I <j

Then exchange a [I] a [J]

Else return J

This seems to be the first version 0.0. First, X is unknown, without any doubt. Then the Left and Right pointers are I j respectively. The left and right pointers are scanned in the middle, J is scanned to a stop less than or equal to X, and I is scanned to a stop greater than or equal to X. The two pointers are exchanged to continue. If we know that the pointer is crossed, return J.

Example (partition1)

1) 2 8 7 1 3 5 6 | 4

I p, J R

 

2) 2 | 8 7 1 3 5 6 | 4

I, p j r

 

3) 2 | 8 | 7 1 3 5 6 | 4

I, p j r

 

4) 2 | 8 7 | 1 3 5 6 | 4

I, p j r

 

5) 2 1 | 7 8 | 3 5 6 | 4

P I j r

 

6) 2 1 3 | 8 7 | 5 6 | 4

P I j r

 

7) 2 1 3 | 8 7 5 | 6 | 4

P I j r

 

8) 2 1 3 | 8 7 5 6 | 4

P I r

 

9) 2 1 3 | 4 | 7 5 6 8

P I r

Well, I can only try my best to achieve this. 0.0 I think everyone should be able to understand it. Each step contains two rows. The first row is the current array state, and the second row is the state of each pointer ." | "Splits the array into several segments. The rightmost "|" indicates that J can only be scanned to 6 instead of the rightmost number. Then the "|" on the leftmost side follows I, which means that the number on the left is smaller than 4. "|" In the middle follows J, that is, all the left side of it has been split. 9), 4 has found the correct position. The left side of the left side is smaller than 4, and the right side is greater than 4. In this case, I + 1 is returned, which is exactly the position of 4 (rows.

 

Fast sorting should be a very important algorithm. It is estimated that we will take the test ~ You can try it by yourself. Haha ~

Http://www.cs.usfca.edu /~ Galles/visualization/comparisonsort.html

As mentioned before, there is a visual quick release.

On the home page, you can share your quick videos. You can take a look at the homepage.

The new semester begins. I wish you good performance in the algorithm data structure ~~

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.