[Introduction to algorithms] learning notes-Chapter 1 linear time sorting

Source: Internet
Author: User

This section mainly demonstrates that for input sequences containing n elements, any comparative sorting must be compared by omega (nlgn) in the worst case. In this way, it is proved that the Merge Sorting and heap sorting are incrementally optimal. At the same time, three sort algorithms for linear time complexity are introduced: Count sort, base sort, and bucket sort.

1. lower bound of the Sorting Algorithm
When determining the lower bound of the sorting algorithm, the decision tree model is used. The decision tree model is a Complete Binary Tree that can compare all elements of a specific Sorting Algorithm with a given input size. For comparison operations, it is assumed that the elements are different, so only the comparison operators smaller than or equal to and greater than are used. Shows a typical decision tree model:

Obviously, n elements, n! And can be thought of as leaf nodes. Therefore
N! <= L <= 2 ^ h, (L indicates the length of the path from the root node to the leaf node, that is, the number of comparisons ).
So h> = lg (N !) = Omega (nlgn ).
8.1-1
The minimum depth of the leaf nodes is n-1, which is an ordered arrangement.

8.1-3
Proof (1) Because n! /2 <= L <= 2 ^ h, so h> = nlgn-nlge-1
(2) Because n! /N <= L <= 2 ^ h, so h> = nlgn-nlge-lgn
(3) because n! /2 ^ n <= L <= 2 ^ h, so h> = nlgn-nlge-n
Therefore, H = omega (nlgn) cannot be linear.

8.1-4
Proof: The question is analyzed first. n elements are composed of N/K subsequences. the subsequences are unordered, but they are ordered. Therefore, the sequence whose length is n is converted to the subsequence whose N/K is composed of k elements. The lower bound of the number of comparisons is Omega (nlgn ).
First, sort K unordered elements. The lower bound of the number of comparisons is Omega (klgk), and the number of comparisons of N/K subsequences is Omega (nlgk ).
After that, you also need to consider the number of comparisons performed on the N/K subsequences for n elements. Because k \ n, it is recommended that n = km, then
(K !) ^ (N/K) <= L <= 2 ^ h, so h> = MLG (k )!, So h> = omega (mklgk) = omega (nlgk)
Therefore, the total number of comparisons is Omega (nlgk ).

2. Count sorting
Count sorting assumes that each of the N inputs is an integer ranging from 0 to K, where k is an integer. When K = O (n), the running time of sorting is Theta (n ).
The basic idea of counting sorting is to determine the number of elements smaller than X for each input element x. With this information, you can directly place X in its position in the output array. If the same element exists, scan from the back to the front. After determining the position of the current element, adjust the count. The code is implemented as follows:

 1 #define MAXN 105 2 #define MAXK 100 3  4 int A[MAXN], B[MAXN];   // A[]: original numbers, B[]: sorted numbers. 5  6 void CountingSort(int A[], int B[], int n, int k) { 7     int C[MAXK+1]; 8     int i, j; 9 10     for (i=0; i<=k; ++i)11         C[i] = 0;12 13     for (j=1; j<=n; ++j)14         ++C[A[j]];15     // C[i] now contains the number of elements equal to i.16 17     for (i=1; i<=k; ++i)18         C[i] += C[i-1];19     // C[i] now contains the number of elements less than or equal to i.20 21     for (j=n; j>=1; --j) {22         B[C[A[j]]] = A[j];23         --C[A[j]];24     }25 }

8.2-4
Solution: 1 ~ 19 lines of program, calculate the array C first. The number of [A. B] ranges is C [B]-C [A-1].
Time Complexity T (n) = theta (n) + theta (K) = theta (n + k ).

3. Base sorting
Radix sort is used to specify n d-digits. Each digit has K possible values. For every cycle of the D-cycle in the base sorting, use a stable sorting (such as counting sorting) with the complexity of theta (n + k ), the time complexity of the base sorting is Theta (D (n + k )). The code is implemented as follows:

 1 void CountingSort(int A[], int B[], int n, int k, int in) { 2     int C[MAXK+1]; 3     int i, j, tmp, mod=pow(k, in), div = mod/k; 4  5     for (i=0; i<k; ++i) 6         C[i] = 0; 7  8     for (j=1; j<=n; ++j) { 9         tmp = A[j]%mod/div;10         ++C[tmp];11     }12 13     for (i=1; i<k; ++i)14         C[i] += C[i-1];15 16     for (j=n; j>=1; --j) {17         tmp = A[j]%mod/div;18         B[C[tmp]] = A[j];19         --C[tmp];20     }21 }22 23 void RadixSort(int A[], int n, int d) {24     int i;25     int B[MAXN];26 27     for (i=1; i<=d; ++i) {28         if (i & 1)29             CountingSort(A, B, n, 10, i);30         else31             CountingSort(B, A, n, 10, i);32     }33 34     if (d & 1) {35         for (i=1; i<=n; ++i)36             A[i] = B[i];37     }38 }

[Introduction to algorithms] learning notes-Chapter 1 linear time 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.