Counting Sort 1

Sort by

recency

|

267 Discussions

|

  • + 0 comments

    For those who stumbled into the same issue as me, make sure that you understand what a frequency array is first, and the proper way to create one, otherwise you will bang your head into a wall over this problem. I ran into issues because I didn't realize it is supposed to be size 100, for every single case. I was making it the same size as the size as the input array.

  • + 0 comments

    Python solution

    def countingSort(arr):
        freq = [0] * 100
        
        for n in arr:
            freq[n] += 1
            
        return freq
    
  • + 0 comments
    def countingSort(arr):
        # Write your code here
        frequencyArr = [0] * 100
        
        for a in arr:
            frequencyArr[a] += 1
        
        return frequencyArr
    
  • + 1 comment

    I feel like this challenge is a lot easier than the challenges suggested before it, and should probably be one of the first ones given.

  • + 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;
        }