Algorithm sorting (ii) --- two methods of high-speed sorting: Bilateral scanning and unilateral Scanning

Source: Internet
Author: User

First, let's talk about the features of high-speed sorting, such as time complexity O (nlog N), worst time complexity O (N ^ 2), and average time O (nlog N ). since function stack is used, the space complexity is O (lg n), and the worst is O (n ). is an unstable sorting method. The basic idea is the divide and conquer Law, the big http://blog.csdn.net/morewindows/article/details/6684558 speak very clearly, divide and conquer law + dig hole method, I will not say much. That is, if a number is taken as the secondary image, the number on the left is smaller than that on the other side, and the number on the right is greater than that on the other side. Then perform recursion on the same method of the two intervals between them.

In terms of its overall implementation, there are two main ideas: Bilateral scanning and unilateral scanning. The procedure is as follows:

I. Bilateral Scanning

Bilateral scanning is a method in Tan haoqiang's book. I personally think it is better to understand it than the following one. It is also a method used in blog posts. See the following program:

void quickSort1(int* x, int l, int r){if(l < r){int i = l, j = r, key = x[l];while(i < j){while( i < j && x[j] >= key){j--;}if(i < j){x[i++] = x[j];}while(i < j && x[i] <= key){i++;}if(i < j){x[j--] = x[i];}}cout<<"i = " <<i<<" j = "<<j<<endl;x[i] = key;quickSort1(x, l, i-1);quickSort1(x, i+1, r);}}

Bilateral scanning is intuitive. First, we enter the program to infer whether or not l <R. Only when conditions are met can we get in. This is also a necessary condition for recursion. It is necessary to make the function have an end and a boundary. Then, enter the large while loop, and then enter the small while loop. First, find the loop from the right. If the number is greater than the key, the J will be moved to the left. Until the first one does not meet the condition, that is, the first number smaller than the key jumps out of the while loop and places it on the "pitfall" dug on the left. At the same time, let the index of the pit + 1, then start scanning from the left side, find the first number greater than the key, and then fill it in the right side of the pit. Index-1 on the right side, and then scan from the right side. Until the large while loop exists at the end, I = J. That is, a high-speed sorting scan is completed. Then, put the original key in X [I], and put it in X [J. Is I equal to J! Then perform recursion to perform the same operations on the range [L, I-1], [I + 1, R.

Key Points of bilateral sorting:1. The first if must exist, which is the sign of the final recursion. 2. In order to find a number so that the number on the left is greater than it and the number on the right is smaller than it, multiple cycles are required. This loop is a large while loop. 3. Swap is not required for bilateral sorting, that is, exchange is not required.


Ii. Unilateral Scanning

The above bilateral sorting shows a large while loop, which needs to be performed multiple times from both sides. For a single scan, you only need to go from left to right to complete a quick scan.

Void quicksort2 (int x [], int L, int R) {If (L> = r) return; int M = L; For (INT I = L + L; I <= r; I ++) {If (X [I] <X [l]) {swap2 (X [++ m], X [I]);} swap2 (X [L], X [m]); quicksort2 (x, l, m-1); quicksort2 (x, m + 1, R );} void swap2 (Int & A, Int & B) {if (a = B) return; // the result of data exchange to the same address is 0a = a ^ B; B = a ^ B; A = a ^ B ;}
Is the code simpler? The program first deduced, assuming l> = r direct return, this is a meaning of bilateral scan if, it is a sign of the end of recursion. Then, use M to record the leftmost index. The default value here is the first index, that is, the index of X [l. [ Note: The initial m value does not necessarily point to the key !, It only points to the leftmost.] Then go to the scan and start with L + 1. If the value on the right is smaller than the key, the X [++ m] and X [I] will be exchanged. If the value on the right is greater than the key, no matter what operation is performed. Here is a special case. Suppose l = 0, then M = 0. assume that X [1] is less than X [0], so that X [1] and X [1] are exchanged, that is, there is no exchange. If the array is 5 4 3 2 1, the exchange here becomes invalid. Let's look at it later, until the for loop ends, and let the number at the last M point exchange with the original key. For example, in the preceding Figure 5 4 3 2 1, the result of the first fast sorting is 1 4 3 2 5. Only the swap after the for is used. Here M has a special meaning, that is, the number pointing to the rightmost side of the key. So we can use it (X [m]) and key (that is, X [l]) for exchange only after it comes out.

Features of a unilateral scan:

1. procedures need to be exchanged;

2. More bubble colors; the goal of bubble is not to let the maximum number sink to the rightmost, but to let all smaller than the key move left, find the demarcation index m. Exchange the sum of keys.

3. The single-side scan of this version number is the most basic and can be optimized.

I wanted to extract the time consumption difference between the two algorithms. Unfortunately, the execution time of the C ++ program was too difficult to achieve. Complete procedures are attached as follows:

// ================================================ ==================================================================== // Name: quiksort. CPP // Author: Yanzi // version: // copyright: Your copyright notice // Description: Hello world in C ++, ANSI-style // ========================================== ========================================================== = # include <iostream> # include <malloc. h> using namespace STD; void swap1 (int A, int B); void printarray (int * In, int N); void quicksort1 (int * X, int l, int R); // bilateral scan, high-speed sorting void quicksort2 (int x [], int L, int R); // unilateral scan, high-speed sorting void swap2 (Int &, int & B); // exchange, this method must be used on mingw. swap1 is invalid # define N 8 // The length of the array int main () {int * input = NULL; input = (int *) malloc (N * sizeof (INT); If (input = NULL) {cout <"memory overflow" <Endl ;} for (INT I = 0; I <n; I ++) {input [I] = rand ();} // int input [] = {55, 41, 59, 26, 53, 58, 97, 93}; cout <"Raw data:" <Endl; printarray (input, n); quicksort2 (input, 0, N-1 ); printarray (input, n); Return 0;} void swap1 (int A, int B) {int temp = A; A = B; B = temp ;} void printarray (int * In, int N) {If (in = NULL) {return;} For (INT I = 0; I <n; I ++) {cout <"" <in [I];} cout <Endl;} void quicksort1 (int * X, int L, int R) {If (L <R) {int I = L, j = r, key = x [l]; while (I <j) {While (I <J & X [J]> = key) {J --;} if (I <j) {x [I ++] = x [J];} while (I <J & X [I] <= key) {I ++;} if (I <j) {x [j --] = x [I] ;}} cout <"I =" <I <"J =" <j <Endl; X [I] = key; quicksort1 (x, L, I-1 ); quicksort1 (x, I + 1, R) ;}} void quicksort2 (INT X [], int L, int R) {If (L> = r) return; int M = L; for (INT I = L + L; I <= r; I ++) {If (X [I] <X [l]) {swap2 (X [++ m], X [I]) ;}} swap2 (X [L], X [m]); quicksort2 (x, l, m-1); quicksort2 (x, m + 1, R);} void swap2 (Int & A, Int & B) {if (a = B) return; // for data exchange at the same address, the result will be 0a = a ^ B; B = a ^ B; A = a ^ B ;}




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.