Added radixsort algorithm.

This commit is contained in:
Panzki
2017-09-29 22:44:01 +02:00
parent cedbe13111
commit 8b8575d058

53
Sorts/radixSort.js Normal file
View 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);