# Introduction to algorithms Study Notes (6): Counting sorting and base sorting

Source: Internet
Author: User

The expected running time of the two sorting items here is O (n), which should be the lowest time complexity so far.

Count sorting

Counting sorting assuming that each of the N input elements is an integer between 0 and K. Here K is an integer. In the specific implementation, we can take K as N

ItemsElementThe largest one.

The basic idea of counting sorting: For each input element x, determine the number of elements smaller than X. Then, based on this information, place X in the final output array.

. For example, if 20 elements are smaller than X, X is placed at the 21st position of the output array. When elements are the same, this method must be modified,

The specific modifications can be found belowCode.

`# Include <iostream> using namespace STD; void display (int A [], int N); int max (int A [], int N ); void counting_sort (int A [], int B [], int N, int K); int main () {int A ; int B ; int N; while (CIN> N) {for (INT I = 0; I <n; I ++) CIN> A [I]; int K = max (, n); counting_sort (A, B, n, k); display (B, n);} return 0;} void display (int A [], int N) {for (INT I = 0; I <n; I ++) cout <A [I] <"; cout <Endl ;} int max (int A [], int N) {int M = A ; for (INT I = 1; I <n; I ++) {If (M <A [I]) M = A [I];} return m ;} /* The subscript of array A and array B starts from 0 */void counting_sort (int A [], int B [], int N, int K) {int * c = new int [k + 1]; for (INT I = 0; I <= K; I ++) C [I] = 0; for (INT I = 0; I <n; I ++) C [A [I] ++; For (INT I = 1; I <= K; I ++) C [I] = C [I] + C [I-1]; for (INT I = n-1; I> = 0; I --) // In turn, traversal will lose the stability of counting sorting {B [C [A [I]-1] = A [I]; C [A [I] --;} delete [] C ;} `

I think the most obvious disadvantage of counting sorting is that it requires a lot of extra space, especially when the few elements in the array to be sorted are very large.

It will cause a great waste of space. For example, if a sequence is: 1, 2, 6, 8, 3, 0... 100, 65..., then I

The value of K is 1000000000, so that the size of array C is open1000000000, but many of them are not needed.

Base sort

Base sorting is achieved by sorting the number of each bit of each element. First, you must unify the length of the digits of all elements. The digits are short.

And then sort each bit in sequence. After sorting the bit from the first bit to the highest bit, you can get an ordered order.

Column. For more information about the correctness of this method, seeAlgorithmIntroduction. The sorting of each bit can be achieved by counting and sorting, then K can be taken 9 directly.

The base sorting implementation code is as follows:

`# Include <iostream> using namespace STD; void display (int A [], int N); int max (int A [], int N); int max_bit (INT num ); void radix_sort (int A [], int N); int main () {int A ; int B ; int N; while (CIN> N) {for (INT I = 0; I <n; I ++) CIN> A [I]; radix_sort (A, n); display (A, n );} return 0;} void display (int A [], int N) {for (INT I = 0; I <n; I ++) cout <A [I] <"; cout <Endl;} int max (int A [], int N) {int M = A ; for (INT I = 1; I <n; I ++) {If (M <A [I]) M = A [I];} return m ;} int max_bit (INT num) {int bits = 1; while (Num/10) {bits ++; num/= 10;} return bits ;} void radix_sort (int A [], int N) {int * B = new int [N]; int C ; int Radix = 1; int t; int D; D = max_bit (max (A, n); For (INT I = 0; I <D; I ++, Radix * = 10) {/* use counting_sort to sort the array by specific digit */For (Int J = 0; j <10; j ++) C [J] = 0; for (Int J = 0; j <n; j ++) {T = A [J]/Radix % 10; C [T] ++ ;} for (Int J = 1; j <10; j ++) C [J] + = C [J-1]; for (Int J = n-1; j> = 0; j --) {T = A [J]/Radix % 10; B [C [T]-1] = A [J]; c [T] --;}/* copy the sorted array from B to a */For (Int J = 0; j <n; j ++) A [J] = B [J];} Delete [] B ;}`

Related Keywords:

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.