The Full Counting Sort

  • + 1 comment
    public static void countSort(List<List<String>> arr) {
            int middle = arr.size() / 2;
            int temp[] = new int[100];
            
            Map<Integer, List<String>> map = new HashMap<>();
            
            // Traverse the list and populate hashmap
            // with keys being the "weight" of the pair.
            // Also replace the str to "-" if it is between [0..n/2].
            for(int i = 0; i < arr.size(); i++) {
                
                List<String> a = arr.get(i);
                int weight = Integer.parseInt(a.get(0));
                String str = a.get(1);
                
                if(i < middle) {
                    str = "-";
                }
                
                temp[weight]++;
                
                if(map.containsKey(weight)) {
                    map.get(weight).add(str);
                } else {
                    map.put(weight, new ArrayList<String>());
                    map.get(weight).add(str);
                }
            }
            
            // The base of count sort adapted to 
            // this challenge.
            for(int i = 0; i < temp.length; i++) {
                
                int repeat = temp[i];
                
                for (int j = 0; j < repeat; j++) {
                    System.out.print(map.get(i).get(j) + " ");
                }
            }
            
        }