Counting Sort 1

  • + 0 comments

    Java Solution:

        public static List<Integer> countingSort(List<Integer> arr) {
            
            Map<Integer, Integer> countMap = new HashMap<>();
            
            for(int i=0; i< arr.size(); i++){
                int current = arr.get(i);
                if(countMap.containsKey(current)){
                    int count = countMap.get(current);
                    countMap.put(current, count+1);
                }else{
                    countMap.put(current, 1);
                }
            }
            
            List<Integer> list = new ArrayList<>(100);
            for(int i=0; i<100; i++){
                if(countMap.containsKey((i))){
                    list.add(countMap.get(i));
                }else{
                    list.add(0);
                }
            }
            
            return list;
        }