The most common sort - Quick Sort

Quick sort proposed by CAR Hoare in 1960. The basic idea is: a trip by ordering the data to be sorted into two independent parts, the part where all the data than the other part of all data to be small, then this method of data for the two parts separately fast sorting, sorting the entire process can be recursively, in order to achieve the whole data into an ordered sequence. It is provided to sort the array A [0] ...... A [N-1], select any of a first data (the first number is usually chosen array) data as a key, and then all of it is smaller than the number of which are placed in left, all larger than its numbers are put to its right, a process known as a trip to quick sort. It is noteworthy that, quick sort is not a stable sorting algorithm, that is, the same relative positions of the plurality of values ​​may fluctuate at the end of the algorithm.

Trip (partition) Fast sorting algorithms are: 1) Set two variables i, j, sort begins when: i = 0, j = N-1; 2) to the first array element as the key data, the assignment to the key, i.e., key = a [0]; 3) Search forward starting from j, i.e., after the start of the search from the front (J,), the first to find a value of less than key a [j], the a [j ] and a [i] value exchange; 4) i from the backward search beginning, i.e. before the start of the backward search (i ++), is greater than a first key to find the a [i], the a [i] and a [ j] value exchange; 5) repeat steps 3 and 4 until i = j; (3,4 step, did not find qualified value, i.e. 3 a [j] is not less than key, 4 in a [i ] when the key change is not greater than j, the value of i such that j = j-1, i = i + 1, until you find found qualified value, when the exchange for i, j pointer position unchanged. Further, i == j when this process must exactly i + j- or completed, so that at this time the end of the cycle).

On a bubble sort can be said is that we learn the first real sorting algorithm, and solves the problem of bucket sort of a waste of space, but in the execution efficiency of the algorithm has sacrificed a lot, it's time complexity reaches O (N2) . If we can run the computer per 10 million times, then the 1 Yi number of sort, bucket sort only need 0.1 seconds, while the bubble sort will need a ten million seconds, reaching 115 days long, is not it scary. That there is neither a waste of space and can be a little faster sorting algorithm it? That is, "quick sort" it! Just listen to this name is not very high-end feel it.

Suppose we now " 61279345108 " is the number of sort 10. First, just to find a number in this sequence as a reference number (do not be scared term is used to refer to a number, you know it will be used in the Zuosha). For convenience, let the first number 6 as the reference number of bars. Next, the sequence number larger than the reference number of all necessary to place 6 on the right, is smaller than the reference count on the 6 on the left, similar to this arrangement.

3 1 2 5 4 6 9 7 10 8

In the initial state, number 6 in the first sequence . 1 bits. Our goal is to 6 moved to somewhere in the middle of the sequence, assuming that this position is k . Now we need to find the k , and with the first k bits demarcation point, from the left are less than or equal 6 , have the right number greater than or equal 6 . Think about it, there are ways you can do it?

Give you a hint of it. Recall that bubble sort, is how "swap" step by step so that each number homing. At this point you can also by means of "exchange" to achieve their goals. Step by step exactly how to exchange it? How to exchange only convenient and save time? Do not rush to look down, he took out a pen, drawing on paper look. First learning bubble sort algorithm when my high school, I felt the bubble sort is a waste of time, each time only to compare two numbers adjacent, which is obviously too unreasonable. So I thought of a way, and later I realized that this is the "quick sort", please allow me a little narcissistic (^ o ^).

The method is simple: respectively, from the initial sequence " 61279345108 ends Start" probe "." First from the right to left to find a less than 6 numbers, then from the left to the right to find a larger than 6 numbers, then swap them. Here two variables i and J , respectively, the leftmost and rightmost point sequence. We played a good name "Sentinel i" and "Sentinel j" for these two variables. At first i let the sentry point to the left-most sequences (ie, i = 1 ), point numbers 6 . Let Sentinel j points to the right-most sequences (i.e., j = 10 ), a digital point 8 .

picture3.1

First Sentinel j started to take action. Because the reference number set here is the leftmost number, you need to let sentry j first out, it is very important (please think about why). Sentinel j step by step to the left to move (ie J - ), until you find a less than 6 numbers to stop. Next Sentinel i then move step by step to the right (ie, i ++ ), until it finds a number greater than 6 numbers to stop. Finally Sentinel j stopped in digital 5 front, Sentinel i stopped in digital 7 before.

picture3.2

picture3.3

Now exchange Sentinel i and sentinel j values pointed elements. After the exchange of the following sequence.

6 1 2 5 9 3 4 7 10 8

picture3.4

picture3.5

This, for the first time the exchange is completed. Then start sentinel j continue to move to the left (Friendly reminder again, each must be sentinels j before departure). He discovered 4 (baseline than 6 after smaller, to meet the requirements) stopped. Sentinel i also move further to the right, he found a 9 (baseline than 6 after the larger, meet the requirements) stopped. At this exchange again, after the exchange of the following sequence.

6 1 2 5 4 3 9 7 10 8

End of the second exchange, "probe" to continue. Sentinel j continue to move to the left, he found a 3 (than the reference number 6 is smaller, to meet the requirements) and later stopped. Sentinel i continue to move to the right, Goodness me! At this time, Sentinel i and Sentinel j met, Sentinel i and Sentinel j have reached 3 front. Description at this time "detection" end. We will reference number 6 and 3 are exchanged. After the exchange of the following sequence.

3 1 2 5 4 6 9 7 10 8

picture3.6

picture3.7

picture3.8

This first round of the "probe" is really over. At this time, the reference number 6 is a cut-off point 6 numbers are less than or equal to the left of 6 , 6 right number are greater than or equal 6 . Just look at the process, in fact Sentinel j 's mission is to find less than the reference count, and sentinel i 's mission is to find the reference count is greater than, until i and j meet so far.

OK, interpretation is completed. Now the reference number 6 has been homing, it is just at the first sequence of 6 bits. At this point we have the original sequence to 6 as a boundary point split into two sequences, the sequences left " 31254 ", the right sequence is " 97,108 ." Next, also we need to address these two sequences. Because 6 left and right of the sequence currently still very confusing. But it does not matter, we have mastered the method, just as long as the next method were treated simulate 6 sequences to the left and right. For now to handle the 6 sequences left of it now.

The sequence left " 31,254 ." This sequence is set to 3 with reference number is adjusted so that the 3 number on the left are less than or equal 3 , 3 right number are greater than or equal 3 . Well I started writing it.

If you are modeling is not their fault, order of the sequence after the adjustment is completed should be.

2 1 3 5 4

OK, now 3 has homing. Next, the processing needs to 3 sequence of the left " 21 " and the right of the sequence " 54 ." Sequence " 21 " to 2 for baseline adjusted, after the end of the processing sequence is " 12 ", this 2 has homing. Sequence " 1 " is only a number, you do not need any treatment. At this point we sequence " 21 " have been processed to obtain sequence is " 12 ." Sequence " 54 " is performed also modeled this method, the resulting sequence is as follows.

1 2 3 4 5 6 9 7 10 8

For sequence " 97108 " is also just simulation procedure until an inseparable until a new sequence. The final sequence will be such as follows.

1 2 3 4 5 6 7 8 9 10

This sort completely finished. Observant students may have found that every round of rapid processing is actually sort of a benchmark of these homing, until all the numbers are the property of bit far, sorting is over. FIG previous vested process will be described below under the overall algorithm.

picture3.9

The quick sort of faster, because compared to bubble sort, each exchange is leaps and bounds. Every time when sorting a set reference point, the reference point will be less than equal to the number of all placed on the left of the reference point, the reference point will be greater than the number equal to the total reference into the right point. So every time the exchange will not be the same as the bubble sort can only be exchanged between adjacent numbers from the exchange of the big more. Therefore, the total number of comparison and exchange have less natural speed would be increased. Of course, in the worst case, it may still be number two adjacent were exchanged. So the worst time complexity of quick sort and bubble sort is the same is O (N2) , its average time complexity of O (NlogN) . In fact, quick sort is based on something called the "binary" thinking. We will encounter later in "two" thought, time to talk to you. First the code as follows.


    #include <stdio.h>
    int a[101],n;//定义全局变量,这两个变量需要在子函数中使用
    void quicksort(int left,int right)
    {
    int i,j,t,temp;
    if(left>right)
       return;

    temp=a[left]; //temp中存的就是基准数
    i=left;
    j=right;
    while(i!=j)
    {
       //顺序很重要,要先从右边开始找
       while(a[j]>=temp && i<j)
    j--;
       //再找右边的
       while(a[i]<=temp && i<j)
    i++;
       //交换两个数在数组中的位置
       if(i<j)
       {
    t=a[i];
    a[i]=a[j];
    a[j]=t;
       }
    }
    //最终将基准数归位
    a[left]=a[i];
    a[i]=temp;

    quicksort(left,i-1);//继续处理左边的,这里是一个递归的过程
    quicksort(i+1,right);//继续处理右边的 ,这里是一个递归的过程
    }
    int main()
    {
    int i,j,t;
    //读入数据
    scanf("%d",&n);
    for(i=1;i<=n;i++)
       scanf("%d",&a[i]);
    quicksort(1,n); //快速排序调用

    //输出排序后的结果
    for(i=1;i<=n;i++)
    printf("%d ",a[i]);
    getchar();getchar();
    return 0;
    }

The following data can be entered for verification

1061279345108

Operating results are

12345678910

Here is an array during program execution in a normalized position of the reference count of the change process, underlined representation.


    1 2 7 9 3 4 5 10 8
    1 2 5 4 6 9 7 10 8
    1 3 5 4 6 9 7 10 8
    2 3 5 4 6 9 7 10 8
    2 3 5 4 6 9 7 10 8
    2 3 4 5 6 9 7 10 8
    2 3 4 5 6 9 7 10 8
    2 3 4 5 6 8 7 9 10
    2 3 4 5 6 7 8 9 10
    2 3 4 5 6 7 8 9 10
    2 3 4 5 6 7 8 9 10

Quick sort in 1960, proposed by CAR Hoare (Tony Hoare, Charles Antony Richard Hoare), then there are a lot of people to do further optimization. If you are interested can go and see quick sort Tony Hoare Computer Journal in 1962 published a paper "Quicksort" and "Introduction to Algorithms" Chapter VII. Fast sorting algorithms are only Tony Hoare in the computer field for the first time to reveal, and later he was the boss of her and reuse, the company wanted him to design a new high-level language for the new machine. You know these had not yet advanced stuff PASCAL, or C language. Later, Tony Hoare participated by Edsger Wybe Dijkstra (1972 Turing Award winner, this great God behind us will encounter time and then small talk) organized "ALGOL 60" training courses, he felt sure its not go design a new language, not as good as the existing "ALGOL 60" to improve, so that it can be used on the new machine company. So he designed a "ALGOL 60" is a subset of the release. This version will premier at the time the various versions of "ALGOL 60" in the implementation of efficiency and reliability, Tony Hoare by the international academic attention. He later "ALGOL X" design also invented the well-known "case" statement, but later widely adopted various high-level languages ​​such as PASCAL, C, Java language and so on. Of course, Tony Hoare contribution in the field of computers and many, he received the Turing Award in 1980.


http://wiki.jikexueyuan.com/project/easy-learn-algorithm/fast-sort.html

Guess you like

Origin www.cnblogs.com/bqwzx/p/11028976.html