Count sort (counting-sort)--Introduction to Algorithms (9)

Source: Internet
Author: User

1. Comparing the lower bounds of the sorting algorithm

(1) Compare sort

So far, we have introduced several algorithms that can sort N in O (NLGN) Time: Merge sort and heap sort have reached the upper bounds of worst case, and fast sort achieves that upper bound on average.

If we look closely, we will find that in the final result of the sort, the order between the elements depends on the comparison between them. We collectively refer to this sort of sorting algorithm as a sort of comparison . The sort algorithm we've introduced so far is a sort of comparison. Let's argue with the fact that any comparison sort algorithm will be compared in the worst case by Ω (n lgn) .

(2) Decision tree Model

Before we prove it, we first introduce a decision tree model that is abstracted from comparative sorting. A decision tree is a fully binary tree, which can represent the comparison of all elements in a given input scale, while controls, data movement, and so on are ignored. For example, it shows the decision tree case where the insertion sorting algorithm acts on an input sequence containing three elements.

(3) worst case of the Nether

From the decision tree we can see that the length of the longest simple path from the root node to any reachable leaf node represents the worst-case comparison in the corresponding sorting algorithm. Therefore, the worst-case order in a comparison sorting algorithm is equal to the height of the decision tree. Also, when all the permutations in the decision tree appear in the form of reachable leaf nodes, the lower bound of the decision tree height is the lower bound of the time to compare the sorting algorithm. Here we give a formal proof.

Consider a tree with a height of h and a decision tree with l reachable leaf nodes. It corresponds to a sort of comparison of n elements. Because the input data is n! The possible permutations are leaf junctions, so the n! ≤l. Since the number of leaf nodes in a two-fork tree with a height of H is no more than 2^h, we get:

N! ≤l≤2^h,

Take the logarithm on both sides:

H≥LG (n!) =ω (NLGN)

2. Counting sort

Let us first assume that the elements of the ordered sequence are all on the interval [0, K].

The idea of counting sort is: In order to sort the sequence, if we can count how many elements are less than or equal to an element, we know the correct position of the element. For example, for the sequence {2,5,3,0,2,3,0,3} to be sorted, we have counted 8 elements less than or equal to 5 (including 5 itself), then 5 this element should be sorted to the 8th bit.

The pseudo-code description for the algorithm is given below:

Where array a[1~n] is the array to be sorted, and the array b[1~n] is used to store the ordered elements. C[0~K] is used to store the above-mentioned statistics (specifically, C[i] represents the total number of elements in array A, less than or equal to I).

The following diagram depicts the process of ordering the sequence {2,5,3,0,2,3,0,3}:

Here we give the Java implementation code of the algorithm:

 Public Static voidMain (string[] args) {int[] Array = {2, 5, 3, 0, 2, 3, 0, 3};p Rintarray (Countingsort (array, 5));}/** * Count Sort * * @param array * Array to be sorted (assuming the range of each element is 0~max, including 0 and Max) * @param max * The maximum value in the array to be sorted * * Public Static int[] Countingsort (int[] Array,intMax) {int[] result =New int[Array.Length];int[] temp =New int[Max + 1];//The following loop operation completes, the first position of temp holds the total number of elements in the array with the value I for(intI:array) {temp[i]++;}//The following loop operation completes, the first position of temp holds the total number of elements in the array with values less than or equal to I for(inti = 1; i < temp.length; i++) {Temp[i] + = temp[i-1];} for(inti = array.length-1; i >-1; i--) {Result[temp[array[i]]-1] = array[i];temp[array[i]]--;}returnResult;}/** * Print array * / Public Static voidPrintArray (int[] array) { for(intI:array) {System.out.print (i + ") ");} System.out.println ();}

3. Algorithm Analysis

Let's now analyze the time cost of counting sorting.

In pseudo-code, the time cost of line 2nd to 3rd is θ (k), the 4th to 5th line is θ (n), the 7th to 8th line is θ (k), and the 10th to 12th line time is θ (n). Therefore, the total run time is θ (k+n). When k= O (n), the run time is θ (n).

It can be seen that the lower bound of the counting sort is better than the lower bound time Ω (NLGN) of the comparison ranking algorithm we demonstrated above. This is because the count sort is not a comparison sorting algorithm. In fact, code that compares the size of some two elements has never been seen in code. Instead, the count sort is the actual value of the INPUT element to determine its position in the array. At this point, the model of the comparison sorting algorithm does not apply to the count sort.

Count sort (counting-sort)--Introduction to Algorithms (9)

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.