Created
April 5, 2019 07:00
-
-
Save ungarson/ff2295071d14475069ad9c4fdb15cb8f to your computer and use it in GitHub Desktop.
Counting sort in javascript
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
function countingSort(arr, maxNumber) { | |
let tempArr = new Array(maxNumber + 1).fill(0); | |
let returnArr = []; | |
for (let i = 0; i < arr.length; i++) { | |
tempArr[arr[i]] += 1; | |
} | |
for (let i = 1; i < tempArr.length; i++) { | |
tempArr[i] += tempArr[i - 1]; | |
} | |
for (let i = 0; i < arr.length; i++) { | |
returnArr[tempArr[arr[i]] - 1] = arr[i]; | |
tempArr[arr[i]] -= 1; | |
} | |
return returnArr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment