Created
April 2, 2017 02:41
-
-
Save KarateJB/863aa1da3302174c0ee309cce4d01a88 to your computer and use it in GitHub Desktop.
[TypeScript] Counting Sort
This file contains 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
private countSort( | |
rangeMin: number, rangeMax: number, arr: number[]) { | |
let mappingArr: boolean[] = []; | |
//Initial | |
for (let i = rangeMin; i <= rangeMax; i++) { | |
mappingArr[i] = false; | |
} | |
//Use the mapping array to map the target array | |
for (let i = 0; i < arr.length; i++) { | |
mappingArr[arr[i]] = true; | |
} | |
//Use the mapping array to generate the sorted array | |
let index=0; | |
for (let i = rangeMin; i <= rangeMax; i++){ | |
if (mappingArr[i]==true) | |
arr[index++] = i; | |
} | |
} | |
//Demo | |
//let arr: number[] = [5, 3, 2, 8, 10, 4, 7, 6, 1, 9]; | |
//this.countSort(1, 10, arr); | |
// > 1,2,3,4,5,6,7,8,9,10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment