Quick sorting,

Source: Internet
Author: User

Quick sorting,

Quick sorting was proposed by C. A. R. Hoare in 1962. Its basic idea is: Split the data to be sorted into two independent parts by one sort, and all the data in one part is smaller than all the data in the other part, then, sort the two data parts by using this method. The entire sorting process can be recursive to convert the entire data into an ordered sequence.

For example, an unordered sequence a [7]:

4 6 8 9 3 1 5

L: 0 r: 6

Now we want to sort it in descending order.

1. Define two positions to Mark l and r on the left and right sides of the part to be sorted (here are numbers 0 and 6). First select the first element 4 as a benchmark value for comparison,

2. Find a number greater than 4 from the right to the left. Here is 5. Then point the right tag to 5, that is, a [6] (in fact, the original j mark this ). In this case, we exchange the two numbers a [r] And a [l] marked by r and l. After the exchange is complete, it becomes like this:

5 6 8 9 3 1 4

L: 0 r: 6

3. Find a number smaller than 4 from left to right. Here is 3. Then let the left flag point to it and switch a [l] to a [r]. After the exchange is complete, it becomes like this:

5 6 8 9 4 1 3

L: 4 r: 6

Then, repeat Step 1 and let r move from the current position to the left to find a number greater than 4. Then we find that l and r have met each other, in this way, we get a special array named element 4. the number on the left is always greater than 4, and the number on the right is always less than 4. In this case, we take out two copies of the array, and the left part is all numbers greater than 4, which is a [0] ~. A [3]. Then one copy on the right is all numbers smaller than 4, a [5] ~ A [6].

Then let the two subsequences perform the above operations respectively:

For the left part a [0] ~ A [3]: 5 6 8 9

Select the first number a [0] on the left, that is, 5 as the reference value, and then make all the numbers greater than 5 on the left of 5, make all numbers smaller than 5 to the right of 5.

Expected result: 6 8 9 5

Then, continue the score and recursively execute this process (so that all the numbers greater than the benchmark value are on the left of the benchmark value, and all the numbers smaller than the benchmark value are on the right of the benchmark value ), then, you cannot score again.

Then we can get a descending sequence.

The specific implementation is as follows:

1 // LR indicates the left and right boundary of the part to be sorted. To sort an array of 10 elements, call QuickSort (); 2 void QuickSort (int L, int R) 3 {4 // set two bits, respectively, for the array subscript 5 int l = L, r = R-1 of the start and end elements to be sorted; 6 if (l> = r) return; 7 int key = a [L]; // use the first element of the sequence as the reference. 8 while (l <r) 9 {10 // Let the subscript r on the right find the first number less than the key from right to left 11 while (a [r] <= key & r> l) r --; // find a value 12 swap (& a [r], & a [l]) greater than the key from the right to the left; // exchange the values of the left and right subscript positions 13 if (l = r) break; // if l is equal to r, end the loop 14 // Let the subscript l on the left find the first number greater than the key from left to right 15 while (a [l]> = key & r> l) l ++; // find a value smaller than the key from left to right 16 swap (& a [r], & a [l]); // exchange the value 17 if (l = r) break of the left and right subscript positions; // if l is equal to r, end the cycle 18} 19 20 // the above section has made r = l, 21 // and all the numbers smaller than the key are on the left of the key, all the numbers greater than the key are on the right side of the key 22 QuickSort (L, r); 23 QuickSort (r + 1, R); 24}

The above code implements descending order. If you want to implement ascending order, you only need to swap the >=and <=in the two while.

Here is an example where 50000 numbers are randomly generated and then output in descending order.

# Include <stdio. h> # include <time. h> # include <stdlib. h> # define N 50000int a [N]; void init () // initialization Array {srand (unsigned) time (NULL); for (int I = 0; I <N; I ++) {a [I] = rand () ;}} void print () // output array element {int I; for (I = 0; I <N; I ++) printf ("% d", a [I]); printf ("\ n");} void swap (int *, int * B) // exchange the values of two variables {int c = * a; * a = * B; * B = c ;} // LR indicates the left and right boundary of the part to be sorted. To sort an array of 10 elements, call QuickSort (); void QuickSort (int L, int R) {// set the double mark, which is the array subscript int l = L, r = R-1; if (l> = r) return; int key = a [L]; // use the first element of the array as the benchmark while (l <r) {// Let the subscript r on the right find the first number less than the key from the right to the left while (a [r] <= key & r> l) r --; swap (& a [r], & a [l]); // exchange the value if (l = r) break of the left and right subscripts; // if l is equal to r, end the loop // Let the subscript l on the left find the first number greater than the key from left to right while (a [l]> = key & r> l) l ++; swap (& a [r], & a [l]); // exchange the value if (l = r) break of the left and right subscripts; // if l is equal to r, end the loop} // The above part has made r = l, // and all the numbers smaller than the key are on the left of the key, all the numbers greater than the key are on the right side of the key: QuickSort (L, r); QuickSort (r + 1, R) ;}int main () {init (); printf ("the array was originally like this: \ n"); print (); QuickSort (0, N); printf ("\ n after sorting: \ n "); print (); system ("pause ");}

 

Related Article

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.