Count sort (counting-sort)

Source: Internet
Author: User

Counting sorting is a stable sorting algorithm, which is not a comparative sorting algorithm. There are restrictions on counting sorting: the number of sorting must be n numbers ranging from 0 to K, so counting sorting is not suitable for sorting letters. Time complexity of counting sorting: O (N + k), space complexity: O (k). When k = N, the time complexity can reach O (n ).

 

Counting sorting: Given A nonordered array, first find the maximum number in the array, and then open up an auxiliary array to map the number in the unordered array to the secondary array, calculate the number of occurrences of each number. Then, we continue to get each position from the auxiliary array. The number of the array to be sorted appears several times, and then the corresponding unordered array is assigned to another array.

 

Sorting Process:

 

A: 2 5 3 0 2 3 0 3 C: 2 0 2 3 0 1

Location: 0 1 2 3 4 5 6 7 Location: 0 1 2 4 5

 

C: 2 2 4 7 7 8

Location: 0 1 2 3 4 5

 

B: 3 C: 2 2 4 6 7 8

Location: 0 1 2 3 4 5 6 7 Location: 0 1 2 4 5

 

B: 0 3 C: 1 2 4 6 7 8

Location: 0 1 2 3 4 5 6 7 Location: 0 1 2 4 5

 

B: 0 3 3 C: 1 2 4 5 7 8

Location: 0 1 2 3 4 5 6 7 Location: 0 1 2 4 5

 

B: 0 0 2 2 3 3 5

Location: 0 1 2 3 4 5 6 7

 

Sample Code:

 

# Include <stdio. h> # include <stdlib. h> # include <string. h> void countingsort (INT arraya [], int arrayb [], int N) // sort the Count {int max = 0; For (INT I = 0; I <N; I ++) // find the maximum number in the array {If (arraya [I]> MAX) max = arraya [I];} int * arrayc = (int *) malloc (sizeof (INT) * (MAX + 1); // open up an array space to store the number of occurrences of each number of memset (arrayc, 0, sizeof (INT) * (MAX + 1); // initialize the opened array to 0 for (INT I = 0; I <n; I ++) // calculate the number of occurrences of each number. arrayc [arraya [I] ++; For (INT I = 1; I <= max; I ++) // accumulate the number of times to know the number, there are several to sort the number of arrayc [I] + = arrayc [I-1]; for (INT I = n-1; i> = 0; I --) // sorts data from high to low, ensuring stability {arrayb [arrayc [arraya [I]-1] = arraya [I]; arrayc [arraya [I] --;} Free (arrayc); // release memory} int main () {int N; int arraya [100], arrayb [100]; printf ("Enter the size of the array to be sorted:"); scanf ("% d", & N); printf ("Enter the numbers in the array :"); for (INT I = 0; I <n; I ++) scanf ("% d", & arraya [I]); countingsort (arraya, arrayb, N ); for (INT I = 0; I <n; I ++) printf ("% d", arrayb [I]);}

 

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.