Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Radix sort in Javascript?
The radix sort algorithm distributes integers into buckets based on a number's significant digit or value (the radix). The radix is based on the number system of the values of the arrays. Let us look at how it can be implemented −
Example
function radixSort(arr) {
// Find the max number and multiply it by 10 to get a number
// with no. of digits of max + 1
const maxNum = Math.max(...arr) * 10;
let divisor = 10;
while (divisor < maxNum) {
// Create bucket arrays for each of 0-9
let buckets = [...Array(10)].map(() => []);
// For each number, get the current significant digit and put it in the respective bucket
for (let num of arr) {
buckets[Math.floor((num % divisor) / (divisor / 10))].push(num);
}
// Reconstruct the array by concatinating all sub arrays
arr = [].concat.apply([], buckets);
// Move to the next significant digit
divisor *= 10;
}
return arr;
}
console.log(radixSort([5,3,88,235,65,23,4632,234]))
Output
[ 3, 5, 23, 65, 88, 234, 235, 4632 ]
Advertisements