Now order the 9 numbers for "6 1 2 7 3 4 5 10 8 10". First, find a number in this sequence as the base number. For convenience, let the first number 6 be the base number. Next, you need to put all the numbers in this sequence that are larger than the base number on the right side of 6, the number smaller than the base number to the left of 6, similar to the following arrangement:
3 1 2 5 4 6 9 7 10 8
In the initial state, the number 6 is in the 1th digit of the sequence. Our goal is to move 6 somewhere in the middle of the sequence, assuming that this position is K. Now you need to look for this k, and with the K-bit as the cutoff point, the number on the left is less than or equal to 6, the right number is greater than or equal to 6. Think about it, do you have a way to do this?
Start probing from each end of the initial sequence "6 1 2 7 9 3 4 5 10 8". Find a number that is less than 6 from right to left, then find a number greater than 6 from left to right, then swap them. Here you can use two variables I and J, pointing to the leftmost and rightmost of the sequence, respectively. We have a nice name for these two variables "Sentinel I" and "Sentinel J". At first let Sentinel I point to the leftmost (ie i=1) of the sequence, pointing to the number 6. Let Sentinel J point to the far right of the sequence (that is, =10), pointing to the number.
First Sentinel J started out. Because the number of datums set here is the leftmost number, it is important to have Sentinel J go first, which is very significant ( think about why ). Sentinel J moves to the left (that is, j--) one step at a pace until a number less than 6 is found to stop. The Sentinel I then moves to the right (that is, i++), until a number greater than 6 is found to stop. The Last Sentinel J stopped in front of the number 5, and Sentinel I stopped at the number 7.
Now swap the values of the elements pointed to by Sentinel I and Sentinel J. The sequence after the interchange is as follows:
6 1 2 5 9 3 4 7 10 8
To this, the first exchange ends. Next, Sentinel J continues to move to the left (again, it must be the Sentinel J to start each time). He found 4 (smaller than the base number 6, met the requirements) and stopped. Sentinel I also continued to move to the right, and he found 9 (larger than the base number 6 to meet the requirements) and then stopped. At this point the interchange is exchanged again, followed by the following sequence:
6 1 2 5 4 3 9 7 10 8
The second exchange ends, "probing" continues. Sentinel J continued to move to the left, and he found 3 (smaller than the base 6, which met the requirements) and then stopped. Sentry I continues to move to the right, bad! At this point Sentinel I and Sentinel J met, Sentinel I and Sentinel J all walked to 3. Description the "Probe" ends at this point. We swapped the benchmark number 6 and 3. The sequence after the interchange is as follows:
3 1 2 5 4 6 9 7 10 8
To this first round of "probing" really ended. At this point the base number 6 is the cutoff point, the number of the left 6 is less than or equal to 6, 6 to the right of the number is greater than or equal to 6. Recalling the process just now, the mission of Sentinel J is to find a number less than the base number, and Sentinel I's mission is to find a number larger than the base number, until I and J meet.
The base number 6 is now returned to the 6th position in the sequence. At this point we have split the original sequence into two sequences with 6 as the dividing point, the sequence on the left is "3 1 2 5 4" and the right sequence is "9 7 10 8". The next step is to process the two sequences separately.
Fast sorting is faster because each exchange is a jump-through compared to the bubble sort. Set a datum point each time it is sorted, place a number less than or equal to the datum point to the left of the Datum point, and place the number greater than or equal to the datum point to the right of the datum point. This will not be the same as the bubble in each exchange of the same time can only be exchanged between the adjacent number, the exchange distance is much larger. Therefore, the total comparison and the number of exchanges is less, the speed naturally increased. Of course, in the worst case, it is still possible to exchange two consecutive numbers. So the worst time complexity for fast sorting is the same as the bubble sort is O (N2), which has an average time complexity of O (Nlogn). In fact, the quick sort is based on a thought called "dichotomy". We will also encounter the "two points" of thought, then chat. First on the code, as follows
1#include <stdio.h>2 inta[101],n;//define global variables, which need to be used in child functions3 voidQuicksortintLeftintRight )4 { 5 inti,j,t,temp;6 if(left>Right )7 return; 8 9Temp=a[left];//the base number is stored in temp.TenI=Left ; Onej=Right ; A while(i!=j) - { - //The order is important, start looking from the right. the while(A[j]>=temp && i<j) -j--; - //and find the right. - while(A[i]<=temp && i<j) +i++; - //swap the position of two numbers in the array + if(i<j) A { att=A[i]; -a[i]=A[j]; -a[j]=T; - } - } - //Finally, the base number is returned to the position ina[left]=A[i]; -a[i]=temp; to +Quicksort (left,i-1);//proceed to the left, here is a recursive process -Quicksort (i+1, right);//proceed to the right, here is a recursive process the } * intMain () $ { Panax Notoginseng inti,j,t; - //read in Data thescanf"%d",&N); + for(i=1; i<=n;i++) Ascanf"%d",&A[i]); theQuicksort1, n);//Quick Sort Call + - //results after sorting the output $ for(i=1; i<=n;i++) $printf"%d", A[i]); - GetChar (); GetChar (); - return 0; the}
View Code
The quick sort was proposed by C. A. R. Hoare (Donnijol, Charles Antony Richard Hoare) in 1960, after which many further optimizations were made. If you are interested in a quick sort, you can look at the paper "Quicksort" published by Donnijol in Computer Journal in 1962 and the seventh chapter of the introduction to algorithms. The fast sorting algorithm was only the first exposure of Donnijol in the computer field, and later he was appreciated and reused by the boss, who wanted to design a new high-level language for the new machine. You should know that there is no such thing as Pascal or C language. Later Donnijol took part in the "ALGOL 60" training course, held by Edsger Wybe Dijkstra (1972 Turing Award winner, the Great God we will meet later), and he felt that he was not so sure of designing a new language as to the existing "ALGOL 60 "To be improved so that it can be used on the company's new machine. So he designed a subset version of "ALGOL 60". This version of the implementation of efficiency and reliability in the then "ALGOL 60" of the various versions of the top, so Donnijol by the international academic attention. Later, he also invented the familiar "case" statement in the "ALGOL X" design, which was later widely used in a variety of high-level languages, such as Pascal, C, the Java language, and so on. Of course, Donnijol has a lot of contributions to the computer field, and he won the Turing Award in 1980.
Recursive-Quick Sort quicksort