Counting Sort 1

Sort by

recency

|

275 Discussions

|

  • + 0 comments

    include

    using namespace std;

    void countingSort(int a[], int n) { int freq[100] = {0}; for (int i = 0; i < n; i++) { freq[a[i]]++; } for (int i = 0; i < 100; i++) { cout << freq[i] << " "; } cout << endl; } int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } countingSort(a, n); return 0; }

  • + 0 comments

    Golang

    func countingSort(arr []int32) []int32 {
        frequency := make([]int32, 100)
        for _, v := range arr {
            frequency[v]++
        }
        return frequency
    }
    
  • + 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])
    }