# Quick sequencing of simple algorithm learning

Source: Internet
Author: User

Transferred from: http://developer.51cto.com/art/201403/430986.htm

Especially easy to understand, especially thanks to bloggers: Ah Halle

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 Ten 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?

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 ten 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 ten 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 Ten 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

`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}`

Quick sequencing of simple algorithm learning

Related Keywords:

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.