Skip to content

Instantly share code, notes, and snippets.

@cozzbie
Created January 10, 2020 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cozzbie/0f0ec1f0620e222002247e04488c78c9 to your computer and use it in GitHub Desktop.
Save cozzbie/0f0ec1f0620e222002247e04488c78c9 to your computer and use it in GitHub Desktop.
const countsort = (array, k) => {
const frequency = [...Array(k + 1).keys()].map(k => 0);
const out = [];
for(let j in array){
frequency[array[j]] = frequency[array[j]] + 1;
}
for(let i = 1; i < frequency.length; i++){
frequency[i] = frequency[i] + frequency[i - 1];
}
for(let i = array.length - 1; i >= 0; i--){
out[frequency[array[i]] - 1] = array[i];
frequency[array[i]] = frequency[array[i]] - 1;
}
return out;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment