Skip to content

Instantly share code, notes, and snippets.

@vadimkorr
Created March 10, 2018 19:12
Show Gist options
  • Select an option

  • Save vadimkorr/7e2c05b95eb8ccd1818795b26c5155b9 to your computer and use it in GitHub Desktop.

Select an option

Save vadimkorr/7e2c05b95eb8ccd1818795b26c5155b9 to your computer and use it in GitHub Desktop.
// Implement counting sort algorithm
// Complexity O(n+k)
// let toNum = (ch) => ch.charCodeAt(0);
// let toChar = (num) => String.fromCharCode(num);
// const RANGE = 255; // numbers, letters
let countingSort = function(input)
{
let inter = [];
let output = [];
input.map(i => {
inter[i] = inter[i] ? inter[i]+1 : 1;
});
inter.map((i, ind) => {
for(let k=0; k<i; k++) {
output.push(ind);
}
})
return output;
}
console.log(countingSort([4, 7, 7, 2, 8, 1, 9]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment