Counting Sort 1

Sort by

recency

|

268 Discussions

|

  • + 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
    
  • + 0 comments
    def countingSort(arr):
        # Write your code here
        frequencyArr = [0] * 100
        
        for a in arr:
            frequencyArr[a] += 1
        
        return frequencyArr
    
  • + 1 comment

    I feel like this challenge is a lot easier than the challenges suggested before it, and should probably be one of the first ones given.