Created
January 10, 2020 21:42
-
-
Save cozzbie/0f0ec1f0620e222002247e04488c78c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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