Skip to content

Commit 497d7df

Browse files
authored
Update README.md
1 parent 2c890d1 commit 497d7df

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

sortingAlgo/countingSort/README.md

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,82 @@
11
# COUNTING SORT
22
The lower bound nlogn does not apply to algorithms that do not compare array
33
elements but use some other information. An example of such an algorithm is
4-
counting sort that sorts an array in O(n) time assuming that every element in
5-
the array is an integer between 0... c and c = O(n).
4+
counting sort that sorts an array in `O(n)` time assuming that every element in
5+
the array is an integer between `0... c` and `c` = `O(n)`.
66
The algorithm creates a bookkeeping array, whose indices are elements of the
77
original array. The algorithm iterates through the original array and calculates
88
how many times each element appears in the array.
99
## Logic
1010
For example, the array
1111

12-
1 3 6 9 9 3 5 9
12+
`1 3 6 9 9 3 5 9`
1313

1414
corresponds to the following bookkeeping array:
1515

16-
1 0 2 0 1 1 0 0 3
17-
1 2 3 4 5 6 7 8 9
16+
`1 0 2 0 1 1 0 0 3`
17+
18+
`1 2 3 4 5 6 7 8 9`
1819

1920
For example, the value at position 3 in the bookkeeping array is 2, because
2021
the element 3 appears 2 times in the original array.
21-
Construction of the bookkeeping array takes O(n) time. After this, the sorted
22-
array can be created in O(n) time because the number of occurrences of each
22+
Construction of the bookkeeping array takes `O(n)` time. After this, the sorted
23+
array can be created in `O(n)` time because the number of occurrences of each
2324
element can be retrieved from the bookkeeping array. Thus, the total time
24-
complexity of counting sort is O(n).
25+
complexity of counting sort is `O(n)`.
2526
Counting sort is a very efficient algorithm but it can only be used when the
2627
constant c is small enough, so that the array elements can be used as indices in
2728
the bookkeeping array.
2829

30+
## Pseudo-code
31+
* Iterate the input array and find the maximum value present in it.
32+
* Declare a new array of size max+1 with value 0
33+
* Count each and every element in the array and increment its value at the corresponding index in the auxiliary array created
34+
* Find cumulative sum is the auxiliary array we adding curr and prev frequency
35+
* Now the cumulative value actually signifies the actual location of the element in the sorted input array
36+
* Start iterating auxiliary array from 0 to max
37+
* Put 0 at the corresponding index and reduce the count by 1, which will signify the second position of the element if it exists in the input array
38+
* Now transfer array received in the above step in the actual input array
39+
40+
## Algorithm
41+
```
42+
countingSort(arr, n)
43+
maximum <- find the largest element in arr
44+
create a count array of size maximum+1
45+
initialise count array with all 0's
46+
for i <- 0 to n
47+
find the total frequency/ occurrences of each element and
48+
store the count at ith index in count arr
49+
50+
for i <- 1 to maximum
51+
find the cumulative sum by adding current(i) and prev(i-1) count and store
52+
it in count arr itself
53+
54+
for j <- n down to 1
55+
copy the element back into the input array
56+
decrement count of each element copied by 1
57+
```
2958
## Time Complexity
3059

3160
### Best Case:
3261
The best case time complexity occurs when all elements are of the same range that is when k is equal to 1.
3362
In this case, counting the occurrence of each element in the input range takes constant time and then finding
34-
the correct index value of each element in the sorted output array takes n time, thus the total time complexity reduces to O(1 + n) i.e O(n) which is linear.
63+
the correct index value of each element in the sorted output array takes n time, thus the total time complexity reduces to `O(1 + n)` i.e `O(n)` which is linear.
3564

36-
N is the number of elements
37-
K is the range of elements (K = largest element - smallest element)
65+
* N is the number of elements
66+
* K is the range of elements (K = largest element - smallest element)
3867

3968
### Worst Case:
4069
Worst case time complexity is when the data is skewed that is the largest element is significantly large than other elements. This increases the range K.
4170

42-
As the time complexity of algorithm is O(n+k) then, for example, when k is of the order O(n^2), it makes the time complexity O(n+(n^2)), which essentially
43-
reduces to O( n^2 ) and if k is of the order O(n^3), it makes the time complexity O(n+(n^3)), which essentially reduces to O( n^3 ).
44-
Hence, in this case, the time complexity got worse making it O(k) for such larger values of k. And this is not the end. It can get even worse for further larger values of k.
71+
As the time complexity of algorithm is `O(n+k)` then, for example, when k is of the order `O(n^2)`, it makes the time complexity `O(n+(n^2))`, which essentially
72+
reduces to `O( n^2 )` and if k is of the order `O(n^3)`, it makes the time complexity `O(n+(n^3))`, which essentially reduces to `O( n^3 )`.
73+
Hence, in this case, the time complexity got worse making it `O(k)` for such larger values of k. And this is not the end. It can get even worse for further larger values of k.
4574
Thus the worst case time complexity of counting sort occurs when the range k of the elements is significantly larger than the other elements.
4675

4776
## Space complexity
4877

49-
In the above algorithm we have used an auxiliary array C of size k, where k is the max element of the given array. Therefore the space complexity of Counting Sort algorithm is O(k).
78+
In the above algorithm we have used an auxiliary array C of size k, where k is the max element of the given array. Therefore the space complexity of Counting Sort algorithm is `O(k)`.
5079

51-
Space Complexity : O(k)
80+
* Space Complexity : `O(k)`
5281

5382
Larger the range of elements in the given array, larger is the space complexity, hence space complexity of counting sort is bad if the range of integers are very large as the auxiliary array of that size has to be made.

0 commit comments

Comments
 (0)