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

Source: Internet
Author: User

First, let's talk about the features of fast sorting, such as time complexity O (nlog N), worst time complexity O (N ^ 2), and average time O (nlog N ). because function stacks are 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 Control Law, the big http://blog.csdn.net/morewindows/article/details/6684558 speak very clearly, divide and Control Law + digging pit method, I will not say much. That is, if a number is used as a reference, the number on the left is smaller than him, and the number on the right is greater than him. Then take the same method for the left and right of the two intervals for recursion.

In terms of its overall implementation, there are two main ideas: Bilateral scanning and unilateral scanning. The program is described 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 unilateral scanning. It is also the method used in blog posts. See the program below:

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 very intuitive. First, you can go to the program to determine whether or not l <R. Only when the conditions are met will you go 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 from the right, as long as the number is greater than the key, the J always moves 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 pitfall + 1, then start scanning from the left, find the first number greater than the key, and then fill it in the pitfall on the right. 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 quick 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 operation 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 fast sorting.

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 makes a judgment. If l> = r returns directly, this means that if is a bilateral scan, 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 from L + 1. If the right side is smaller than the key, convert X [++ m] to X [I. If the value on the right is greater than the key, no operation is performed. Here is a special case. If l = 0, m = 0. if X [1] is less than X [0], X [1] and X [1] are exchanged, which means no exchange is performed. 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. This version of unilateral scan is the most basic and can be optimized.

I wanted to measure the time consumption difference between the two algorithms. Unfortunately, it was too difficult for C ++ to get the program running time. The complete program is attached below:

// ================================================ ==================================================================== // 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, quick sorting void quicksort2 (int x [], int L, int R); // unilateral scan, quick 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.