Algorithm Learning ranking algorithm (five) (high-speed sorting)

Source: Internet
Author: User

1. Algorithm idea

Set to sort the array is a[0] ... A[n-1], start by selecting a data (usually the first number of the array) as the important data, and then put all the smaller numbers in front of it. All the numbers larger than it are put behind it. This process is called a high-speed sequencing. It is important to note that high-speed sequencing is not a stable sorting algorithm. In other words, the relative position of multiple identical values may change at the end of the algorithm.

A high-speed sequencing algorithm is:
1) Set two variables I, J. When the sort starts: i=0. J=n-1.
2) Assign a value to key as the first array element as the important data. i.e. Key=a[0].
3) Search forward from J, that is, start searching forward (J –), find the first value less than key a[j], interchange a[j] and A[i].
4) from I start backward search, that is, from the front start backward search (i++), find the first greater than key A[i], will a[i] and A[j] interchange;
5) Repeat 3rd, 4 steps until i=j; (3,4 step, no value is found for the condition.) That is, 3 a[j] is not less than key,4 a[i] is not larger than the time to change the value of J, I, so that j=j-1,i=i+1 until found. Locate the value that matches the criteria. At the time of the exchange I, the J pointer position is unchanged. Other than that. I==j This process must be exactly when the i+ or J is finished, at which time the loop ends).

2. Implementation code

Simplify the idea of the previous algorithm, the process such as the following
The basic idea of this method is:
1. First, a number is taken from the series as the base number.
2. The partitioning process, which puts the number of large numbers on its right, is less than or equal to its number to the left.


3. Repeat the second step to the left and right intervals until there is only one number for each interval.
Although high-speed sequencing is called a divide-and-conquer method, the three-word divide-and-conquer method clearly does not summarize all the steps of high-speed sequencing.

So my high-speed sequencing is further explained: Pit filling + divide-and-conquer method:

Summarize the number of digging pits

1. I =l; j = R; Gouge out the base number to form the first pit a[i].
2. J – from the back forward to find a number smaller than it, found after digging out of this number before filling a pit a[i].
3. i++ from the front to find a larger number than it, find and dig out this number to fill the previous pit a[j].
4. Run 2, 32 steps again and again until i==j, filling in the base number into a[i].

intAdjustarray (intS[],intLintR//Returns the position of the adjusted base number{inti = l, j = r;intx = S[l];//s[l] That is the first pit of S[i]     while(I < J) {//From right to left to find less than X number to fill s[i]         while(I < J && S[j] >= x) j--;if(I < J) {S[i] = s[j];//S[J] into s[i], s[j] form a new piti++; }//left to right to find greater than or equal to the number of X to fill s[j]         while(I < J && S[i] < x) i++;if(I < J) {S[j] = s[i];//s[i] into s[j], s[i] form a new pitj--; }    }//exit. I equals J. Fill in the hole with X. S[i] = x;returni;}voidQuick_sort1 (intS[],intLintR) {if(L < R) {inti = Adjustarray (S, L, R);///first into the pit filling method adjustment s[]Quick_sort1 (S, l, I-1);//Recursive invocationQuick_sort1 (s, i +1, R); }}
3, improved version of the code
//high-speed sequencingvoidQuick_sort (intS[],intLintR) {if(L < R) {//swap (S[l], s[(l + R)/2]);//will be in the middle of this number and first number exchange See note 1        inti = l, j = r, x = S[l]; while(I < J) { while(I < J && S[j] >= x)//Find the first number less than x from right to leftj--;if(I < j) s[i++] = S[j]; while(I < J && S[i] < x)//left to right find the first number greater than or equal to xi++;if(I < j) s[j--] = s[i];        } S[i] = x; Quick_sort (S, l, I-1);//Recursive invocationQuick_sort (s, i +1, R); }}

"Analytic" high-speed sequencing because the sorting efficiency is more efficient in several sorting methods of the same O (N*logn), it is often used.

Algorithm Learning ranking algorithm (five) (high-speed sorting)

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.