Counting Sort 1

Sort by

recency

|

128 Discussions

|

  • + 0 comments

    C++

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

    Java:

    public static List countingSort(List arr) { Integer[] arrayInteger = new Integer[100]; Arrays.fill(arrayInteger, 0); for (Integer integer : arr) { arrayInteger[integer]++; }
    return Arrays.asList(arrayInteger); }

  • + 0 comments
    def countingSort(arr):
        freq = [0] * 100
        
        for i in arr:
            freq[i] += 1
        
        return freq
    
  • + 2 comments

    This problem is just meaningless... Why not create an array with the length of max value in the given array plus one.

  • + 0 comments

    My TypeScript solution:

    function countingSort(arr: number[]): number[] {
        const frequencyArray = (new Array(100)).fill(0);
        const length = arr.length;
        
        for (let i = 0; i < length; i++) {
            frequencyArray[arr[i]] += 1;
        }
    
        return frequencyArray;
    }