
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Radix sort - JavaScript
Radix Sort
Radix sort is a sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value.
We are required to write a JavaScript function that takes in an array of literals as the only argument. The function should sort the array in either increasing or decreasing order using the radix sort algorithm.
Example
Following is the code −
const arr = [45, 2, 56, 2, 5, 6, 34, 1, 56, 89, 33]; const radixSort = (arr = []) => { const base = 10; let divider = 1; let maxVal = Number.NEGATIVE_INFINITY; while (divider === 1 || divider <= maxVal) { const buckets = [...Array(10)].map(() => []); for (let val of arr) { buckets[Math.floor((val / divider) % base)].push(val); maxVal = val > maxVal ? val : maxVal; } arr = [].concat(...buckets); divider *= base; }; return arr; }; console.log(radixSort(arr));
Output
Following is the output on console −
[ 1, 2, 2, 5, 6, 33, 34, 45, 56, 56, 89 ]
- Related Articles
- Radix sort in Javascript?
- Radix Sort
- Java Program for Radix Sort
- C Program for Radix Sort
- C++ Program to Implement Radix Sort
- What is JSlint error "missing radix parameter" in JavaScript?
- Parse and format to arbitrary radix
- JavaScript Sort() method
- Merge sort vs quick sort in Javascript
- Natural Sort in JavaScript
- Convert BigInteger into another radix number in Java
- Sort Array of numeric & alphabetical elements (Natural Sort) JavaScript
- Using merge sort to recursive sort an array JavaScript
- Digital root sort algorithm JavaScript
- Implementing Priority Sort in JavaScript

Advertisements