Counting Sort 1

Sort by

recency

|

467 Discussions

|

  • + 0 comments
    def countingSort(arr):
        # Write your code here
        
        r = [0] * 100
        for i in arr:
            r[i] += 1
           
        return r
    
  • + 0 comments

    Here is problem solution in python java c++ c and javascript - https://programmingoneonone.com/hackerrank-counting-sort-1-problem-solution.html

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/snADwNvEEcM

    vector<int> countingSort(vector<int> arr) {
        vector<int> count(100, 0);
        for(int i = 0; i < arr.size(); i++) count[arr[i]]++;
        return count;
    }
    
  • + 0 comments

    My Java solution with o(n) time complexity and o(1) space complexity:

    public static List<Integer> countingSort(List<Integer> arr) {
            // init freq array
            int[] freqArr = new int[100];
            
            //update freq for each val
            for(int element : arr) freqArr[element]++;
            
            //return int array converted into Integer list
            return Arrays.stream(freqArr)
                .boxed()
                .collect(Collectors.toList());
        }
    
  • + 0 comments

    Here is my simple Python solution!

    def countingSort(arr):
        frequency = [0 for i in range(100)]
        for num in arr:
            frequency[num] += 1
        return frequency