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 */
348public 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 ;
0 commit comments