Counting Sort 2

  • + 0 comments

    Javascript:

    function countingSort(arr) {
       let maxNum = Math.max(...arr) + 1;
       let countArray = Array(maxNum).fill(0);
       let resultArray = Array();
       
       arr.forEach((num) => {
          countArray[num]++ 
       });
    
       countArray.forEach((result, index) => {
           for (var i = 0; i < result; i++) resultArray.push(index);
       });
       
       return resultArray;
    }