Counting Sort 1

Sort by

recency

|

270 Discussions

|

  • + 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
    
    }
    
  • + 0 comments

    My Rust approach:

    fn countingSort(arr: &[i32]) -> Vec { let mut my_vec: Vec = vec![0; 100];

    for var in arr {
        if let Some(x) = my_vec.get_mut(*var as usize) {
            *x += 1;
        }
    }
    
    my_vec
    

    } `

  • + 0 comments

    For those who stumbled into the same issue as me, make sure that you understand what a frequency array is first, and the proper way to create one, otherwise you will bang your head into a wall over this problem. I ran into issues because I didn't realize it is supposed to be size 100, for every single case. I was making it the same size as the size as the input array.

  • + 0 comments

    Python solution

    def countingSort(arr):
        freq = [0] * 100
        
        for n in arr:
            freq[n] += 1
            
        return freq