Created
March 10, 2018 19:12
-
-
Save vadimkorr/7e2c05b95eb8ccd1818795b26c5155b9 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
| // 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