const ITEMS = 20; let list = new Array(ITEMS); function countingSort(){ let count = []; let min = Math.min(...list); let max = Math.max(...list); console.log("Original list: " + list); console.log("Init counts: "); for(var i = min; i <= max; i++){ count[i] = 0; console.log(i + " : " + count[i]); } for(var i = 0; i < list.length; i++){ count[list[i]]++; } console.log("Final Counts: "); for(var i=min; i<=max; i++){ console.log(i + " : " + count[i]); } let j = 0; for(var i = min; i <= max; i++){ while(count[i] > 0){ list[j] = i; j++; count[i]--; } } console.log("Sorted list: " + list); } function initList() { for (let i = 0; i < ITEMS; i++) list[i] = Math.round(Math.random() * 100); } initList(); countingSort();