Knowledge in quick sorting: the process of quick sorting

Source: Internet
Author: User
Through the previous questions and the introduction of the concept of "information entropy", we can re-understand the nature of sorting: a group of unordered N numbers, a total of N! Sort, only one of which meets the meaning (for example, from large to small ). In other words, there are N possibilities for sorting problems! Type. Any basic operation unit based on comparative sorting is "compare a and B", which is equivalent to guessing a question in a digital game.

Through the previous questions and the introduction of the concept of "information entropy", we can re-understand the nature of sorting:

A group of N unordered numbers. they have N! Sort, only one of which meets the meaning (for example, from large to small ).

In other words, there are N possibilities for sorting problems! Type. Any basic operation unit based on comparative sorting is "compare a and B", which is equivalent to guessing a question in a digital game, obviously, the answer to this question can only be "yes" or "no". a question with only two outputs can only cut the possibility space into half. according to the above ideas, the best cut method is to cut 1/2 and 1/2 (cut the array into half ). That is to say, we hope that after comparing the relationship between a and B, if we find that a <B, the possibility of the remaining arrangement will become N! /2. if a> B is found, N is left! /Two possibilities.

Assuming that the probability of each sort is equal, this means that a <B is supported for a total of N! /2. N is supported for a> B! /2. In other words, the probability of a <B is equal to the probability of a> B.

We hope that the probability of a <B and a> B is equal every time we compare a and B, in this way, we can ensure that the possibility can be reduced to the lower limit of the original half.

A direct inference is that, if every time we make a perfect comparison like the above, then the N of the N elements! Possible arrangement only requires log2N! The troubleshooting is complete, while log2N! Similar to NlogN. This is exactly the complexity of the fast sorting.

Implementation of quick sorting

Let's take a look at the working mechanism of quick sorting. below is the fast sorting in Introduction to Algorithms:

QUICKSORT (A, p, r) if p <rthen q PARTITION (A, p, r) // key QUICKSORT (A, p, q-1) QUICKSORT (, q + 1, r)

The key to the fast sorting algorithm is the PARTITION process, which rearranges A [p. r] in place:

PARTITION(A, p, r)  x ← A[r]  i ← p - 1  for j ← p to r - 1       do if A[j] ≤ x             then i ← i + 1                  exchange A[i] <-> A[j]  exchange A[i + 1] <-> A[r]  return i + 1

We will describe the above process in C language:

# Include "stdio. h "# include" math. h "# include" stdlib. h "void PrintArray (int * arr); void swap (int * a, int * B); int num = 10; void QuickSort (int * arr, int beg, int end) {if (beg <end) {int Partition = Partition (arr, beg, end); QuickSort (arr, beg, latency-1); QuickSort (arr, latency + 1, end) ;}} void swap (int * a, int * B) {int tmp; tmp = * a; * a = * B; * B = tmp ;} int Partition (int * arr, int beg, int end) {int j; int sentinel = arr [end]; printf ("\ n sentinel = arr [% d] = % d", end, sentinel); int I = beg-1; for (j = beg; j <= end-1; ++ j) {if (arr [j] <= sentinel) {printf ("\ n arr [% d] (% d) <= sentinel (% d) ", j, arr [j], sentinel); I ++; swap (& arr [I], & arr [j]);} swap (& arr [I + 1], & arr [end]); printf ("\ n sorting process:"); PrintArray (arr); return I + 1 ;} void PrintArray (int arr []) {int I; for (I = 0; I <num; ++ I) {printf ("% d ", arr [I]) ;}} int main () {int I; int arr [10]; srand (time (0); for (I = 0; I <10; I ++) {arr [I] = rand () % 100 + 1; // printf ("% d", rand () % 100 + 1 );} printf ("initial array:"); PrintArray (arr); QuickSort (arr, 0, num-1); printf ("\ n Final result:"); PrintArray (arr ); return 0 ;}
Initial array: 59 40 55 92 73 69 27 79 3 30 sentinel = arr [9] = 30 arr [6] (27) <= sentinel (30) arr [8] (3) <= sentinel (30) sorting process: 27 3 30 92 73 69 59 79 40 55 sentinel = arr [1] = 3 sorting process: 3 27 30 92 73 69 59 79 40 55 sentinel = arr [9] = 55 arr [8] (40) <= sentinel (55) sorting process: 3 27 30 40 55 69 59 79 92 73 sentinel = arr [9] = 73 arr [5] (69) <= sentinel (73) arr [6] (59) <= sentinel (73) sorting process: 3 27 30 40 55 69 59 73 92 79 sentinel = arr [6] = 59 sorting process: 3 27 30 40 55 59 69 73 92 79 sentinel = arr [9] = 79 sorting process: 3 27 30 40 55 59 69 73 79 92 final result: 3 27 30 40 55 59 69 73 79 92 Process returned 0 (0x0) execution time: 0.564 sPress any key to continue.

From the running results of the program, we can clearly see the work project of quick sorting:

  1. Set sentinel as the last element 30 of the array.
  2. Divide a group (, 30) smaller than 30 and put them in front of the array.
  3. The fixed sentinel is set to the last of the group, excluding 30, namely 3 in (27,3.
  4. Sort the group in situ, that is, (3, 27 ). This completes the sorting of the group (, 30 ).
  5. The fixed point sentinel is set to the last element 55 of the array again.
  6. Divide the element smaller than 55 into another group (40 ).
  7. The sorting of the group has been completed ).
  8. Set the fixed point to 73, group (, 73), and sort to (, 73 ).
  9. Set a fixed point to 79, group (79)
  10. Sorted: 3 27 30 40 55 59 69 73 79 92

To sum up the process of fast sorting: randomly select an element as the "axis element" (the above-mentioned sentinel), move all the elements smaller than the axis to the left, and move the others to the right. According to this process, the first comparison of the quick sort is to compare an element with the axis element. it is obvious that the possibility of "greater than" and "less than" is half. This is a pretty comparison.

Additional reading

The topic list of this article is as follows:

  1. Knowledge in quick sorting: start with guesses
  2. Knowledge in quick sorting: Let's look at the question of ball.
  3. Knowledge in quick sorting: Information entropy
  4. Knowledge in quick sorting: the process of quick sorting
  5. Knowledge in quick sorting: Hall and quick sorting
  6. Knowledge in quick sorting: Implementation of Hall's fast sorting
  7. Knowledge in quick sorting: Key element selection and algorithm efficiency
  8. Knowledge in quick sorting: randomization fast sorting

Address of this article: http://www.nowamagic.net/librarys/veda/detail/2390,welcome to the original release.

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.