Nodejs Array Radix Sort radixSort(maxDigit)

Here you can find the source of radixSort(maxDigit)

Method Source Code

Array.prototype.radixSort = function(maxDigit) {
   var dev = 1;//w w  w .  ja v  a 2  s.c o m
   var mod = 10;
   var arr= [];
   for (var i = 10; i >= 0; i--) {
      arr[i] = [];
   }
   for(var i = 0; i < maxDigit; i++,dev *= 10, mod *= 10) {
      for(var z = 0; z < this.length; z++) {
         var bucket = Math.floor((this[z] % mod) / dev);
            arr[bucket].push(this[z]);
      }
      var p = 0;
      for(var y = 0; y < arr.length; y++) {
         if(arr[y].length) {
            while(arr[y].length > 0) {
               this[p++] = arr[y].shift();
            }
         }
      }
   }
   return this;
}

var a = [2,3,4,8,5];
a.radixSort(1);
console.log(a);
var b = [123,25,67,25,137,225,24,467];
b.radixSort(3);
console.log(b);

Related

  1. radixSort()
    Array.prototype.radixSort = function () {
      var key, i, j, stack;
      var bucket = [[].concat(this)];
      var sorted = false;
      var digits = 1;
      while (!sorted) {
        var nextBucket = [];
        sorted = true;
        var nextDigits = digits * 10;
    ...