Counting Sort 2

Sort by

recency

|

391 Discussions

|

  • + 0 comments

    here is my Python solution

    def countingSort(arr):
        freq=[0]*100
        s_arr=[]
        for i in arr:
            freq[i]+=1
        
        for i in range(len(freq)):
            num=[i]*freq[i]
            s_arr.extend(num)
            
        return s_arr
    
  • + 0 comments

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

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

    Here is my Python solution!

    def counting(arr):
        frequency = [0 for i in range(100)]
        for num in arr:
            frequency[num] += 1
        return frequency
    
    def countingSort(arr):
        frequency = counting(arr)
        sortedarr = []
        for i in range(len(frequency)):
            for j in range(frequency[i]):
                sortedarr.append(str(i))
        return sortedarr
    
  • + 0 comments

    function countingSort(arr) { return arr.sort((a,b)=>a-b);

    }

  • + 0 comments

    My Python3 solution:

    def countingSort(arr: list[int]) -> list[int]:
        # Write your code here
        freq = [0] * 100
        for num in arr:
            freq[num] += 1
    
        x: list[int] = [i for i in range(100) for _ in range(freq[i])]