|
| 1 | +# Count Sort |
| 2 | + |
| 3 | +## Example Problem |
| 4 | + |
| 5 | +Given an array `arr = [a1,a2,a3,a4,a5...,an] ` , we need to sort the array with the help of Count Sort algorithm. |
| 6 | + |
| 7 | +<hr> |
| 8 | + |
| 9 | +## Discussion |
| 10 | + |
| 11 | +Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values. Then doing some arithmetic to calculate the position of each object in the output sequence. |
| 12 | + |
| 13 | +Take a count array of size ` max_element + 1` and initialise the array with 0 . Now iterate through the initial array and for each element x increase the value of count array at index x by 1. |
| 14 | + |
| 15 | +But this method won't work for negative integers . |
| 16 | + |
| 17 | +So we take a count array of size `max_element - min_element + 1` and initialize with 0. Now iterate through the initial array and for each element x increase the value of count array at index `x - min_element` by 1. |
| 18 | + |
| 19 | +Now Iterate through the count array and if value at the `index + min_element` is greater than 0 keep on adding the index value to the array , and decrement the value of the index by 1. |
| 20 | + |
| 21 | +<hr> |
| 22 | + |
| 23 | +## Working of the Algorithm |
| 24 | + |
| 25 | +``` |
| 26 | +Consider array = [1 , 3 , -5 , 3 , 4 , -2 , 0] |
| 27 | +
|
| 28 | +Here max_element = -5 and min_element = 4 |
| 29 | +
|
| 30 | +So, Create an array of size (4 - (-5) + 1) = 10 and initialise with 0. |
| 31 | +
|
| 32 | +count_array = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] |
| 33 | +idx_values-> 0 1 2 3 4 5 6 7 8 9 |
| 34 | +
|
| 35 | +After the iteration , count array becomes -> |
| 36 | +
|
| 37 | +count_array = [ 1 , 0 , 0 , 1 , 0 , 1 , 1 , 0 , 2 , 1 ] |
| 38 | +idx_values-> 0 1 2 3 4 5 6 7 8 9 |
| 39 | +
|
| 40 | +The Sorted Array becomes : [-5 , -2 , 0 , 1 , 3 , 3 , 4] |
| 41 | +``` |
| 42 | + |
| 43 | +<hr> |
| 44 | + |
| 45 | +## Code |
| 46 | + |
| 47 | +### Code For Count Sort |
| 48 | + |
| 49 | +```cpp |
| 50 | +void CountSort(int a[],int n) |
| 51 | +{ |
| 52 | + int max_element = -INT_MAX , min_element = INT_MAX ; |
| 53 | + |
| 54 | + // for finding the max_element in the array |
| 55 | + for(int i=0;i<n;i++) |
| 56 | + max_element = max(max_element , a[i]); |
| 57 | + |
| 58 | + // for finding the min_element in the array |
| 59 | + for(int i=0;i<n;i++) |
| 60 | + min_element = min(min_element , a[i]); |
| 61 | + |
| 62 | + // initisalizing a count vector of size max_element - min_element + 1 |
| 63 | + vector <int> count(max_element - min_element +1 , 0); |
| 64 | + |
| 65 | + // setting count vector according to the algorithm |
| 66 | + for(int i=0;i<n;i++) |
| 67 | + count[a[i] - min_element]++; |
| 68 | + |
| 69 | + // updating the actual array |
| 70 | + int j = 0,i=0; |
| 71 | + while(i<max_element - min_element +1) |
| 72 | + { |
| 73 | + if(count[i] == 0) |
| 74 | + i++; |
| 75 | + else |
| 76 | + { |
| 77 | + a[j] = min_element + i; |
| 78 | + count[i]--; |
| 79 | + j++; |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | +
|
| 86 | +<hr> |
| 87 | +
|
| 88 | +## Time Complexity |
| 89 | +
|
| 90 | +**Time Complexity**: O(n+k) where n is the number of elements in input array and k is the range of input. |
| 91 | +
|
| 92 | +**Space Complexity**: Algorithm takes extra space of O(k). |
| 93 | +
|
| 94 | +<hr> |
| 95 | +
|
| 96 | +## Important |
| 97 | +Counting sort is efficient if the range of input data is not significantly greater than the number of objects to be sorted. Consider the situation where the input sequence is between range 1 to 10K and the data is 10, 5, 10K, 5K. |
| 98 | +
|
| 99 | +<hr> |
| 100 | +
|
| 101 | +Other Resources : [GFG Blog](https://www.geeksforgeeks.org/counting-sort/) |
0 commit comments