11import java .util .ArrayList ;
2+ import java .util .List ;
23import java .util .Map ;
34import java .util .TreeMap ;
5+ import java .util .stream .IntStream ;
6+ import java .util .stream .Stream ;
7+
8+ import static java .util .stream .Collectors .toList ;
9+ import static java .util .stream .Collectors .toMap ;
410
511/**
612 *
713 * @author Youssef Ali (https://github.com/youssefAli11997)
14+ * @author Podshivalov Nikita (https://github.com/nikitap492)
815 *
916 */
1017
@@ -13,78 +20,87 @@ class CountingSort {
1320 /**
1421 * This method implements the Generic Counting Sort
1522 *
16- * @param array The array to be sorted
17- * @param last The count of total number of elements in array
18- * Sorts the array in increasing order
19- * It uses array elements as keys in the frequency map
23+ * @param list The list to be sorted
24+ *
25+ * Sorts the list in increasing order
26+ * The method uses list elements as keys in the frequency map
2027 **/
21-
22- public static <T extends Comparable <T >> void CS (T [] array , int last ) {
23-
24- Map <T , Integer > frequency = new TreeMap <T , Integer >();
25- // The final output array
26- ArrayList <T > sortedArray = new ArrayList <T >();
27-
28- // Counting the frequency of @param array elements
29- for (T t : array ) {
30- try {
31- frequency .put (t , frequency .get (t )+1 );
32- }catch (Exception e ){ // new entry
33- frequency .put (t , 1 );
34- }
35- }
36-
37- // Filling the sortedArray
38- for (Map .Entry <T , Integer > element : frequency .entrySet ()) {
28+ public static <T extends Comparable <T >> List <T > CS (List <T > list ) {
29+
30+ Map <T , Integer > frequency = new TreeMap <>();
31+ // The final output array
32+ List <T > sortedArray = new ArrayList <>(list .size ());
33+
34+ // Counting the frequency of @param array elements
35+ list .forEach (v -> frequency .put (v , frequency .getOrDefault (v , 0 ) + 1 ));
36+
37+ // Filling the sortedArray
38+ for (Map .Entry <T , Integer > element : frequency .entrySet ()) {
3939 for (int j =0 ; j <element .getValue (); j ++)
4040 sortedArray .add (element .getKey ());
4141 }
42-
43- for (int i =0 ; i <array .length ; i ++){
44- array [i ] = sortedArray .get (i );
45- }
42+
43+ return sortedArray ;
44+ }
45+
46+
47+ /**
48+ * Stream Counting Sort
49+ * The same as method {@link CountingSort#CS(List)} but this method uses stream API
50+ *
51+ * @param list The list to be sorted
52+ *
53+ **/
54+ public static <T extends Comparable <T >> List <T > streamCS (List <T > list ) {
55+ return list .stream ()
56+ .collect (toMap (k -> k , v -> 1 , (v1 , v2 ) -> v1 + v2 , TreeMap ::new ))
57+ .entrySet ()
58+ .stream ()
59+ .flatMap (entry -> IntStream .rangeClosed (1 , entry .getValue ()).mapToObj (t -> entry .getKey ()))
60+ .collect (toList ());
4661 }
4762
4863 // Driver Program
4964 public static void main (String [] args ) {
5065 // Integer Input
51- Integer [] arr1 = {4 ,23 ,6 ,78 ,1 ,54 ,231 ,9 ,12 };
52- int last = arr1 .length ;
53-
54- System .out .println ("Before Sorting:" );
55- for (int i =0 ;i <arr1 .length ;i ++) {
56- System .out .print (arr1 [i ] + " " );
57- }
58- System .out .println ();
66+ List <Integer > unsortedInts = Stream .of (4 , 23 , 6 , 78 , 1 , 54 , 23 , 1 , 9 , 231 , 9 , 12 ).collect (toList ());
5967
60- CS (arr1 , last );
68+ System .out .println ("Before Sorting:" );
69+ printList (unsortedInts );
6170
62- // Output => 1 4 6 9 12 23 54 78 231
71+ // Output => 1 1 4 6 9 9 12 23 23 54 78 231
6372 System .out .println ("After Sorting:" );
64- for (int i =0 ;i <arr1 .length ;i ++) {
65- System .out .print (arr1 [i ] + " " );
66- }
67- System .out .println ();
68-
69- System .out .println ("------------------------------" );
70-
73+ printList (CS (unsortedInts ));
74+ System .out .println ("After Sorting By Streams:" );
75+ printList (streamCS (unsortedInts ));
76+
77+ System .out .println ("\n ------------------------------\n " );
78+
7179 // String Input
72- String [] array1 = {"c" , "a" , "e" , "b" ,"d" };
73- last = array1 .length ;
74-
75- System .out .println ("Before Sorting:" );
76- for (int i =0 ;i <array1 .length ;i ++) {
77- System .out .print (array1 [i ] + " " );
78- }
79- System .out .println ();
80+ List <String > unsortedStrings = Stream .of ("c" , "a" , "e" , "b" ,"d" , "a" , "f" , "g" , "c" ).collect (toList ());
8081
81- CS (array1 , last );
82+ System .out .println ("Before Sorting:" );
83+ printList (unsortedStrings );
8284
83- //Output => a b c d e
85+ //Output => a a b c c d e f g
8486 System .out .println ("After Sorting:" );
85- for (int i =0 ; i <last ; i ++) {
86- System .out .print (array1 [i ]+" " );
87- }
88-
87+ printList (CS (unsortedStrings ));
88+
89+ System .out .println ("After Sorting By Streams:" );
90+ printList (streamCS (unsortedStrings ));
91+
92+ }
93+
94+ /**
95+ * Just print list
96+ * @param toPrint - a list which should be printed
97+ */
98+ private static void printList (List <?> toPrint ){
99+ toPrint .stream ()
100+ .map (Object ::toString )
101+ .map (str -> str + " " )
102+ .forEach (System .out ::print );
103+
104+ System .out .println ();
89105 }
90106}
0 commit comments