Counting Sort 3

  • + 0 comments

    IMHO while the target of this task is to show how pre-sorting is helpful for some other tasks, there is simple JS solution: 1) extract nums into additional array 2) sort asc this array (it doesn't matter how to do this, so I use built-in 'sort' method) 3) calc values in one loop 0..99 using results for previous array items

        let result = new Array();
        let num_arr = arr.map(value => value.split(' ')[0]);
        num_arr.sort((a,b)=>a-b);
    
        let j = 0;
            for (let i=0; i<100; i++){
        while (num_arr[j]<=i) j++;
        result[i] = j;
    }
    
    return result;