Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. const unsortedArray = [123, 49, 7, 333]
  2.  
  3. const radix = (arr) => {
  4. const max = Math.max(...arr).toString().length
  5. const buckets = new Array(10)
  6. let arrayWithLeftPaddedZeros = []
  7.  
  8. for (let i = 0; i < arr.length; i++) {
  9. arrayWithLeftPaddedZeros[i] = arr[i].toString().padStart(max, '0')
  10. }
  11.  
  12. for (let i = 0; i < arrayWithLeftPaddedZeros.length; i++) {
  13. let j = 2
  14. //for (let j = max - 1; j >= 0; j--) {
  15. if (!buckets[arrayWithLeftPaddedZeros[i][j]]) {
  16. buckets[arrayWithLeftPaddedZeros[i][j]] = []
  17. }
  18.  
  19. buckets[arrayWithLeftPaddedZeros[i][j]].push(arrayWithLeftPaddedZeros[i])
  20. // }
  21. }
  22.  
  23. const a = [...buckets]
  24.  
  25. console.log(buckets.toString())
  26. }
  27.  
  28. radix(unsortedArray)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement