Counting Sort 1

Sort by

recency

|

125 Discussions

|

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

    A rust solution:

    fn countingSort(arr: &[i32]) -> Vec<i32> {
        arr.iter()
            .fold(vec![0; 100], |mut acc, &x| {
                acc[x as usize] += 1;
                acc
            })
    }
    
  • + 0 comments
    function countingSort(arr) {
        // Write your code here
        const arrSample = Array(arr.length).fill(0)
        for(let i in arrSample) {
            arrSample[arr[i]] +=1
        }
        return arrSample.slice(0, 100);
    
    }
    
  • + 0 comments

    def countingSort(arr):

    buckets = [0] * 100
    for i in range(len(arr) - 1):
        buckets[arr[i]] += 1
    return buckets