11/**
2- * Algorithms-in -Java
2+ * Algorithms-In -Java
33 * CountingSort.java
44 */
55package com .deepak .algorithms .Sorting ;
66
77/**
8- * Class for CountingSort implementation
8+ * Counting Sort Implementation
9+ *
910 * @author Deepak
1011 */
1112public class CountingSort {
@@ -24,55 +25,42 @@ public static void main(String[] args) {
2425 * Counting sort is a sorting technique based on keys between a specific range.
2526 * It works by counting the number of objects having distinct key values (kind of hashing).
2627 * Then doing some arithmetic to calculate the position of each object in the output sequence.
28+ *
2729 * <p>Notes:
28- * 1. Counting sort is efficient if the range of input data is not significantly greater than the number of objects to be sorted.
29- * Consider the situation where the input sequence is between range 1 to 10K and the data is 10, 5, 10K, 5K.
30- 2. It is not a comparison based sorting. It running time complexity is O(n) with space proportional to the range of data.
31- 3. It is often used as a sub-routine to another sorting algorithm like radix sort.
32- 4. Counting sort uses a partial hashing to count the occurrence of the data object in O(1).
33- 5. Counting sort can be extended to work for negative inputs also.
34- 6. This type of integer sorting algorithms are usually designed to work in either the pointer machine or random access machine models of computing
35- </p>
30+ * 1. Counting sort is efficient if the range of input data is not significantly greater than the number of objects to be sorted.
31+ * - Consider the situation where the input sequence is between range 1 to 10K and the data is 10, 5, 10K, 5K.
32+ * 2. It is not a comparison based sorting. It running time complexity is O(n) with space proportional to the range of data.
33+ * 3. It is often used as a sub-routine to another sorting algorithm like radix sort.
34+ * 4. Counting sort uses a partial hashing to count the occurrence of the data object in O(1).
35+ * 5. Counting sort can be extended to work for negative inputs also.
36+ * 6. This type of integer sorting algorithms are usually designed to work in either the pointer machine or random access machine models of computing
37+ * </p>
3638 */
37- private static void performCountingSort (int [] values ) {
38-
39-
40- int n = values .length ;
41-
42- // The output character array that will have sorted arr
43- int output [] = new int [n ];
44-
45- // Create a count array to store count of inidividul
46- // characters and initialize count array as 0
47- int count [] = new int [256 ];
48- for (int i =0 ; i <256 ; ++i )
49- count [i ] = 0 ;
50-
51- // store count of each character
52- for (int i =0 ; i <n ; ++i )
53- ++count [values [i ]];
54-
55- // Change count[i] so that count[i] now contains actual
56- // position of this character in output array
57- for (int i =1 ; i <=255 ; ++i )
58- count [i ] += count [i -1 ];
59-
60- // Build the output character array
61- for (int i = 0 ; i <n ; ++i )
62- {
63- output [count [values [i ]]-1 ] = values [i ];
64- --count [values [i ]];
65- }
66-
67- // Copy the output array to arr, so that arr now
68- // contains sorted characters
69- for (int i = 0 ; i <n ; ++i )
70- values [i ] = output [i ];
71-
72- System .out .print ("Sorted array is " );
73- for (int i =0 ; i <values .length ; ++i )
74- System .out .print (values [i ]+"," );
39+ private static void performCountingSort (int [] values ) {
40+ /* Find the length of the collection */
41+ int n = values .length ;
42+ /* Output character array which will hold the sorted array */
43+ int output [] = new int [n ];
44+ /* Create a count array of 256 size, and store the count of each character in it */
45+ int count [] = new int [256 ];
46+ for (int i = 0 ; i < n ; ++i ) {
47+ ++count [values [i ]];
48+ }
49+ /* Change count[i] so that count[i] now contains actual position of the character */
50+ for (int i = 1 ; i <= 255 ; ++i ) {
51+ count [i ] += count [i -1 ];
52+ }
53+ /* Build the output character array */
7554
55+ // Build the output character array
56+ for (int i = 0 ; i <n ; ++i ) {
57+ output [count [values [i ]] - 1 ] = values [i ];
58+ --count [values [i ]];
59+ }
60+ System .out .print ("Sorted array is : " );
61+ for (int i = 0 ; i < output .length ; ++i ) {
62+ System .out .print (output [i ]+", " );
63+ }
7664 }
7765
7866}
0 commit comments