Skip to content

Commit 2bc3a4a

Browse files
committed
Revamp counting sort description
1 parent c89b4c4 commit 2bc3a4a

File tree

2 files changed

+29
-42
lines changed

2 files changed

+29
-42
lines changed

src/main/java/algorithms/sorting/countingSort/CountingSort.java

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,8 @@
22

33
/**
44
* <p></p> Stable implementation of Counting Sort.
5-
*
6-
* <p></p> Brief Description: <br>
7-
* Counting sort is a non-comparison based sorting algorithm and isn't bounded by the O(nlogn) lower-bound
8-
* of most sorting algorithms. <br>
9-
* It first obtains the frequency map of all elements (ie counting the occurrence of every element), then
10-
* computes the prefix sum for the map. This prefix map tells us which position an element should be inserted. <br>
11-
* Ultimately, each group of elements will be placed together, and the groups in succession, in the sorted output.
12-
*
13-
* <p></p> Assumption for use: <br>
14-
* To perform counting sort, the elements must first have total ordering and their rank must be known.
15-
*
16-
* <p></p> Implementation Invariant: <br>
17-
* At the end of the ith iteration, the ith element from the back will be placed in its rightful position.
18-
*
19-
* <p></p>
20-
* COMMON MISCONCEPTION: Counting sort does not require total ordering of elements since it is non-comparison based.
21-
* This is incorrect. It requires total ordering of elements to determine their relative positions in the sorted output.
22-
* In fact, in conventional implementation, the total ordering property is reflected by virtue of the structure
23-
* of the frequency map.
24-
*
25-
* <p></p> Complexity Analysis: <br>
26-
* Time: O(k+n)=O(max(k,n)) where k is the value of the largest element and n is the number of elements. <br>
27-
* Space: O(k+n)=O(max(k,n)) <br>
28-
* Counting sort is most efficient if the range of input values do not exceed the number of input values. <br>
29-
* Counting sort is NOT AN IN-PLACE algorithm. For one, it requires additional space to store freq map. <br>
30-
*
31-
* <p></p> Note: Implementation deals with integers but the idea is the same and can be generalised to other objects,
32-
* as long as what was discussed above remains true.
5+
* This non-comparison-based sorting algorithm is efficient for sorting integers with a small range.
6+
* For detailed explanation and complexity analysis, see README.
337
*/
348
public class CountingSort {
359
/**
@@ -40,7 +14,7 @@ public class CountingSort {
4014
public static int[] sort(int[] arr) {
4115
int k = 0;
4216
int n = arr.length;
43-
// Find the max. value in arr
17+
// Find the max. value in arr. This tells us the size of the freq map array.
4418
for (int i = 0; i < n; i++) {
4519
k = Math.max(k, arr[i]);
4620
}
@@ -53,11 +27,11 @@ public static int[] sort(int[] arr) {
5327
for (int i = 1; i < k+1; i++) {
5428
freq[i] += freq[i-1];
5529
}
56-
// Sort the array by placing in output array
30+
// Sort the array by placing element in the output array
5731
int[] sorted = new int[n];
58-
for (int i = arr.length-1; i >= 0; i--) { // Notice we start from the back to maintain stable property
32+
for (int i = arr.length-1; i >= 0; i--) { // we start from the back to maintain stable property
5933
int num = arr[i];
60-
sorted[freq[num]-1] = num;
34+
sorted[freq[num]-1] = num; // freq[num]-1 because 0-indexed
6135
freq[num]--;
6236
}
6337
return sorted;
Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
# Counting Sort
22

3+
## Background
34
Counting sort is a non-comparison-based sorting algorithm and isn't bounded by the O(nlogn) lower-bound
45
of most sorting algorithms. <br>
5-
It first obtains the frequency map of all elements (ie counting the occurrence of every element), then
6+
It first obtains the frequency map of all elements (i.e. counting the occurrence of every element), then
67
computes the prefix sum for the map. This prefix map tells us which position an element should be inserted.
7-
Ultimately, each group of elements will be placed together, and the groups in succession, in the sorted output.
8+
It is updated after each insertion to reflection the new position to insert the next time the same element is
9+
encountered. <br>
10+
11+
### Implementation Invariant
12+
At the end of the ith iteration, the ith element (of the original array) from the back will be placed in
13+
its correct position.
14+
15+
Note: An alternative implementation from the front is easily done with minor modification.
16+
The catch is that this implementation is not stable.
17+
18+
### Common Misconception
19+
_Counting sort does not require total ordering of elements since it is non-comparison based._
20+
21+
This is incorrect. It requires total ordering of elements to determine their relative positions in the sorted output.
22+
In our implementation, the total ordering property is reflected by virtue of the structure of the frequency map.
823

924
## Complexity Analysis
10-
Time: O(k+n)=O(max(k,n)) where k is the value of the largest element and n is the number of elements. <br>
11-
Space: O(k+n)=O(max(k,n)) <br>
25+
**Time**: O(k+n)=O(max(k,n)) <br>
26+
**Space**: O(k+n)=O(max(k,n)) <br>
27+
where k is the value of the largest element and n is the number of elements.
28+
1229
Counting sort is most efficient if the range of input values do not exceed the number of input values. <br>
1330
Counting sort is NOT AN IN-PLACE algorithm. For one, it requires additional space to store freq map. <br>
1431

1532
## Notes
16-
COMMON MISCONCEPTION: Counting sort does not require total ordering of elements since it is non-comparison based.
17-
This is incorrect. It requires total ordering of elements to determine their relative positions in the sorted output.
18-
In fact, in conventional implementation, the total ordering property is reflected by virtue of the structure
19-
of the frequency map.
20-
21-
Supplementary: Here is a [video](https://www.youtube.com/watch?v=OKd534EWcdk) if you are still having troubles.
33+
1. Counting sort (stable version) is often used as a sub-routine for radix sort.
34+
2. Supplementary: Here is a [video](https://www.youtube.com/watch?v=OKd534EWcdk) if you are still having troubles.

0 commit comments

Comments
 (0)