Counting sorting and its extension ideas

Source: Internet
Author: User
(1) Principles and code and time complexity analysis 1. Counting sorting principle: Set the sorted array to A, and store it to B. C is a temporary array. The so-called count is to calculate the number of elements whose size is equal to I through an array C [I]. This process only requires one loop traversal. On this basis, calculating the number of elements smaller than or equal to I is also completed in a loop. The next step is the key: backward loop, from length [a] to 1, place a [I] to Position C [A [I] in B. The principle is: C [A [I] indicates the number of elements smaller than or equal to a [I], which is exactly where a [I] should be sorted. In addition, the reverse cycle from length [a] to 1 ensures that the relative order between the same elements remains unchanged, which also reflects the stability of counting sorting. When array A has an attachment attribute, stability is very important. 2. Prerequisites and applicability of counting sorting: the elements in a cannot be greater than K and must be used as the subscript of the array. Therefore, the elements should be non-negative integers. In addition, if a has a large element, it cannot allocate enough space. Therefore, counting sorting has many limitations. It is mainly applicable to situations where the number of elements is large, but the total number is smaller than K. In this case, counting sorting can achieve high efficiency. 3. algorithm code and test code: [CPP] view plaincopy # include <stdio. h> # include <conio. h> # define MAX 1000 // function prototype void counting_sort (int A [], int length_a, int B [], int K); // test code int main () {int A [] = {-, 3}; // 1 to 10, ten test data: int B [11] = {0}; int K = 10; // all test data is between 0 and K. counting_sort (A, 10, B, k); For (INT I = 1; I <11; I ++) printf ("% d", B [I]); getch ();} // counting sorting void counting_sort (int A [], int length_a, int B [], int K) {Int C [Max] = {0}; // C is a temporary array for (INT I = 1; I <= length_a; I ++) c [A [I] ++; // at this time, C [I] contains the number of elements equal to I for (INT I = 1; I <= K; I ++) c [I] = C [I] + C [I-1]; // At this time C [I] contains the number of elements less than or equal to I for (INT I = length_a; i> = 1; I --) // The Reverse traversal from length_a to 1 is to ensure that the relative order after the same elements are sorted does not change {// If from 1 to length_a, the relative order of the same element is backward, but the result is correct. B [C [A [I] = A [I]; c [A [I] -- ;}} 4. time Complexity Analysis: In counting_sort, there are only three single-repetition cycles, so the time complexity is O (n ). For non-compare sorting, the lower time bound of comparison sorting is O (nlogn) (2) Expansion of Count sorting 1. the idea of extending the sorting of negative numbers: the counting sorting requires that elements can be used as the subscript of the array and cannot be negative. My idea is to separate negative numbers and non-negative numbers, take the absolute value of negative numbers, sort the counts of the two groups separately, and then merge the numbers of the two groups. The time complexity is still O (N), but n will be larger. Of course, all processed are integers. 2. Extended sorting of floating point numbers: floating point numbers cannot be used as array subscripts. I have already negated two methods of comparison. Here I will write an algorithm that I think can be used as the basis, but it still requires a lot of improvement. Store a floating point number in a linked list, where each node stores one bit, and sorts each group of nodes by count. This will lead to the time complexity of O (n2). I have not thought of how to optimize it. If you have any ideas, please tell me, thank you very much. Summary: Counting sorting is not a local sorting. It requires one auxiliary space and stores the results to another space. It is applicable to a relatively narrow range, but has a time complexity of O (n. I now understand that algorithm design is to find an optimal balance between time, space, and applicability. Related Articles: Randomized version of quick sorting Optimization Algorithm of quick sorting recursive algorithm of Merge Sorting heap sorting non-recursive algorithm of Merge Sorting
Http://blog.csdn.net/neilhappy/article/details/7202507

 

Counting sorting and its extension ideas

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.