Counting Sort 1

Sort by

recency

|

273 Discussions

|

  • + 0 comments

    C++20 ` vector countingSort(vector arr) { vector frequency(100); std::fill(frequency.begin(), frequency.end(), 0);

    for (int n : arr){
        frequency[n]++;
    }
    
    for(int n : frequency){
        std::cout<<n<<" ";
    }
    
    return frequency;
    

    } `

  • + 0 comments

    Python

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

    Swift

    func countingSort(arr: [Int]) -> [Int] {
        // Write your code here
        let maxNumber = arr.max()
        var array: [Int] = Array.init(repeating: 0, count: arr.count)
        for item in arr {
            array[item] = array[item] + 1
        }
    
       return Array(array[0..<100])
    }
    
  • + 0 comments
    def countingSort(arr):
        # Write your code here
        freq = [0]*100
        for i in range(len(arr)):
            freq[arr[i]] += 1
        return freq
    
  • + 0 comments

    My approach for Javascript

    function countingSort(arr) {
        const countArr = new Array(100).fill(0);
        
        for(const a of arr) {
            countArr[a]++
        }
        
        return countArr
    
    }