mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
Added radixsort algorithm.
This commit is contained in:
53
Sorts/radixSort.js
Normal file
53
Sorts/radixSort.js
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Radix sorts an integer array without comparing the integers.
|
||||
* It groups the integers by their digits which share the same
|
||||
* significant position.
|
||||
* For more information see: https://en.wikipedia.org/wiki/Radix_sort
|
||||
*/
|
||||
function radixSort(items, RADIX) {
|
||||
|
||||
//default radix is then because we usually count to base 10
|
||||
if (RADIX === undefined || RADIX < 1) {
|
||||
RADIX = 10;
|
||||
}
|
||||
|
||||
var maxLength = false;
|
||||
var placement = 1;
|
||||
|
||||
while (!maxLength) {
|
||||
maxLength = true;
|
||||
var buckets = [];
|
||||
|
||||
for (var i = 0; i < RADIX; i++) {
|
||||
buckets.push([]);
|
||||
}
|
||||
|
||||
for (var j = 0; j < items.length; j++) {
|
||||
var tmp = items[j] / placement;
|
||||
buckets[Math.round(tmp % RADIX)].push(j);
|
||||
if (maxLength && tmp > 0) {
|
||||
maxLength = false;
|
||||
}
|
||||
}
|
||||
|
||||
var a = 0;
|
||||
for (var b = 0; b < RADIX; b++) {
|
||||
var buck = buckets[b];
|
||||
for (var k = 0; k < buck.length; k++) {
|
||||
items[a] = k;
|
||||
a++;
|
||||
}
|
||||
}
|
||||
placement *= RADIX;
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
//Implementation of radixSort
|
||||
|
||||
var ar = [5, 6, 7, 8, 1, 2, 12, 14];
|
||||
//Array before Sort
|
||||
console.log(ar);
|
||||
radixSort(ar);
|
||||
//Array after sort
|
||||
console.log(ar);
|
Reference in New Issue
Block a user