sit on the toilet. Algorithm: Quick Sort
Sorting algorithm of high-fast province
Is there a sort algorithm that doesn't waste space and can be faster? That's "quick sort". Does it feel very high-end to listen to this name?
Let's say we sort the 9 numbers for "6 1 2 7 3 4 5 10 8 10". First of all, in this sequence, look for a number as the base number (do not be frightened by this noun, is a reference to the number, you will know what it is used to do). 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 it.
Ranking algorithm for the display of Divinity
The method is very simple: start "probing" from the initial sequence "6 1 2 7 9 3 4 5 10 8" at each end. 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.
OK, the explanation is complete. 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. Because the 6 left and right sequences are still very confusing at the moment. But it does not matter, we have mastered the method, the next as long as the simulation of the method of the 6 left and right of the sequence can be processed separately. Now let's deal with the sequence of 6 left.
The sequence on the left is "3 1 2 5 4". Adjust this sequence to 3 as the base number so that the number on the left of 3 is less than or equal to 3, and the number on the right of 3 is greater than or equal to 3. All right, start writing.
If you are not wrong in the simulation, the order of the sequence after the completion of the adjustment should be:
2 1 3) 5 4
OK, now 3 has been returned to place. Next you need to deal with 3 left sequence "2 1" and the right sequence "5 4". The sequence "2 1" is adjusted with 2 as the base number, the sequence after processing is "1 2", and the 2 has been returned to the position. The sequence "1" has only one number and does not require any processing. At this point we have finished processing the sequence "2 1", and the resulting sequence is "1 2". The sequence "5 4" is also modeled after this method, with the resulting sequence as follows:
1 2 3 4 5 6 9 7 10 8
For the sequence "9 7 10 8" also simulates the process just now until a new subsequence is not detached. Will eventually get such a sequence, as follows
1 2 3 4 5 6 7 8 9 10
To this, the sort ends completely. Careful classmates may have found that the quick sort of every round of processing is actually the number of the base of the round to the position, until all the numbers are returned, the sorting is over. Below the previous domineering diagram to describe the whole process of the algorithm.
What is this for?
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
#include <stdio.h>
int a[101],n;//defines a global variable that needs to be used in a child function
void quicksort (int left,int right)
{
int i,j,t,temp;
if (left>right)
Return
Temp=a[left]; The base number is stored in temp.
I=left;
J=right;
while (I!=J)
{
The order is important, start looking from the right.
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)
{
T=a[i];
A[I]=A[J];
a[j]=t;
}
}
Finally, the base number is returned to the position
A[left]=a[i];
A[i]=temp;
Quicksort (left,i-1);//continue to handle the left, here is a recursive process
Quicksort (i+1,right);//continue to handle the right, here is a recursive process
}
int main ()
{
int i,j,t;
Read in Data
scanf ("%d", &n);
for (i=1;i<=n;i++)
scanf ("%d", &a[i]);
Quicksort (1,n); Quick Sort Call
Results after sorting the output
for (i=1;i<=n;i++)
printf ("%d", a[i]);
GetChar (); GetChar ();
return 0;
}
You can enter the following data to verify
1061279345108
The running result is
12345678910
Posture Step
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.