Introduction to the algorithm six: Decision tree for linear time sequencing & counting sorting

Source: Internet
Author: User

The first five articles of this series are all related to the comparison sorting algorithm, starting from this article, will enter the linear time sorting. What is the comparison sort, simply put, is that the process of sorting relies on the comparison of the size of the data in the array to determine the position of the data when it is sorted out.

Comparison sorting method is more intuitive, but also has its shortcomings, we are easy to prove any comparison sorting method, in the worst case of the time complexity of the lower limit is NLGN. To prove this problem, we first need to figure out a model: Decision tree Model.


One, decision tree model

What is a decision tree? Decision tree in terms of form, is a completely binary tree, which in addition to the leaf node, the other layers of the nodes are full. Each of its leaf nodes represents a possible sort of combination of input data, while its non-leaf nodes represent a judgment operation. Or an example to illustrate the comparative Image:

For example, for an array with three elements, how many kinds of possibilities does it have? It's not hard to figure out 3*2*1=3!. , so it has 3 leaf nodes in the decision tree! That's 6, because each leaf node represents a sort of possible. So how do these leaf nodes get it? is based on the judging conditions of non-leaf nodes, we let 1 2 3 represent three elements of an array of 3 elements, judging the process


For example, we compare elements 1 and 2, and then according to the result, to determine whether it is into the left or right sub-tree, and then a layer of judgment, and finally get a sort of one of the results, of course, for a certain array, the path can only be one, for example, we use the array [6,8,5] as an example, The path to the Orange arrow is followed by the following: element 3,1,2, or [5,6,8].


According to the nature of the complete binary tree, we can know that for a full binary tree with a height of h, the leaf node has a maximum of 2^h, and we here (for the ordering of N nodes) the number of leaf nodes in the bottom layer of the policy tree is n!, so we are not difficult to get 2^h>=n!, so there are: H>=lg ( n!), also by the algorithm Introduction (third edition) on the formula 3.19 know LG (n!) >=NLGN, so h>=nlgn.


At this point, we can know that for any comparison sorting algorithm, its policy tree height is also nlgn high, that is, it to get a sort of results, at least through the NLGN step, the worst case time complexity of the lower limit is NLGN.


second, counting sorting algorithm

It has a time complexity of O (n) for a count sort, but its use has some additional constraints: n elements of ① input are an integer within the 0~k interval, where k is an integer; ②k=o (n)


For arrays that meet both of these criteria, you can use the Count sort and the time complexity for sorting is O (n).


The counting sort is not like the quick sort before, the heap sort, the comparison between the elements needs to be done. A count sort is a sort method that does not require a comparison of elements at all. Let's take a look at the basic idea of counting sorting:

Because the array to which the count is sorted satisfies the above ① and ② conditions, we can know that the values of the elements in the array are only k+1 possible, that is, from the values of the 0~k. And, for the sort, from small to large, as an example, for a certain element, if the elements in the array are different, if we know how many of the data is smaller than the number of the element, then we can determine the position of the element, if the value of the element is equal, We only need to process the equal elements.

For example, X is an element in an array where 17 elements are less than x, and no element is equal to the value of x, then x in the sorted array should be in the 18th element (from small to large). When there are elements equal to the value of x, you need to adjust the scheme to deal with the order of equal time.


Based on the thought above, for the small to large count sort, we only ask for the number of each element not more than their elements, you can find a way to determine their position after the sorting, that is, can complete the sort. It is conceivable that, in addition to the array a[n [to be sorted], we need an array of b[n] to store the elements of the sorted array, and an array c[k+1] to store the number of occurrences of the k+1 values in the array from 0~k, and to find the sum of the number of occurrences of their smaller values.

Here is a C + + implementation code:


#include <iostream> #define Max_value 7#define min_value 0using namespace std;void countingsort (int a[], int c[], int b[], int max); void PrintArray (int a[], int len); int main () {int a[10] = {5, 4, 0, 1, 6, 4, 0, 1, 7, 3};int b[10];int c[ Max_value + 1];p Rintarray (a), Countingsort (A, C, B, 7);p Rintarray (b); return 0;} /* * Count sort, assuming n number, maximum value is 7, minimum value is 0 */void countingsort (int a[], int c[], int b[], int max) {int tmp;/* initialize C array, in order to make it easier to count the number of elements less than an element later, Because some elements may not appear, the number of them should be initialized to 0 */for (int i = 0; I <= max; i++) {c[i] = 0;} PrintArray (c, 8);//Statistics The number of occurrences of each element in a array for (int i = 0; i <; i++) {c[a[i]]++;}  PrintArray (c, 8);//statistics on how many for (int i = 0; I <= max; i++) {tmp = I-1;IF (tmp < 0) {//Do not do} else {C[i] on elements less than or equal to each element + = C[tmp];}} PrintArray (c, 8);//fill in the sorted array of output into B for (int i = 9; I >= 0; i--) {//minus one is for the array ordinal to be counted from 0 B[c[a[i]] 1] = a[i];c[a[i]]--;//processing has element with the same value as the}}//print array void printArray (int a[], int len) {for (int i = 0; i < len; i++) {cout << a[i] << ';} cout << Endl;} 

analysis of counting and sorting algorithm

According to the above code, it is not difficult to calculate the algorithm complexity of counting sorting: Here the value k=8, because it is [0,7], the number of input elements is n=10, so, O (k) +o (n) +o (k) +o (n) =o (k+n), and when K=o (n), the entire time complexity is O (n).

It can be found that the time complexity of the counting sort is better than the worst-case complexity of the NLGN, but the application scope of the counting sort is narrow.

Another important property of the count ordering is that it is stable, that is, for elements of the same value in the array, the first INPUT element is preceded by the sort, and then the input element is followed by the input sort array to b[n], from the large to the small loop, and the loop is once c[a[i]-- ] to ensure that the input elements are placed in a larger ordinal position. The count sort is often used as a sub-procedure of the cardinality sorting algorithm, where the requirement for a count sort must be stable.




















Introduction to the algorithm six: Decision tree for linear time sequencing & counting 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.