lib/radix-sort.js
function countingSortByDigit(array, radix, exponent, minValue) {
  var i;
  var bucketIndex;
  var buckets = new Array(radix);
  var output = new Array(array.length);

  // Initialize bucket
  for (i = 0; i < radix; i++) {
    buckets[i] = 0;
  }

  // Count frequencies
  for (i = 0; i < array.length; i++) {
    bucketIndex = Math.floor(((array[i] - minValue) / exponent) % radix);
    buckets[bucketIndex]++;
  }

  // Compute cumulates
  for (i = 1; i < radix; i++) {
    buckets[i] += buckets[i - 1];
  }

  // Move records
  for (i = array.length - 1; i >= 0; i--) {
    bucketIndex = Math.floor(((array[i] - minValue) / exponent) % radix);
    output[--buckets[bucketIndex]] = array[i];
  }

  // Copy back
  for (i = 0; i < array.length; i++) {
    array[i] = output[i];
  }

  return array;
}
function countingSortByDigit(array, radix, exponent, minValue) {
  var i;
  var bucketIndex;
  var buckets = new Array(radix);
  var output = new Array(array.length);

  // Initialize bucket
  for (i = 0; i < radix; i++) {
    buckets[i] = 0;
  }

  // Count frequencies
  for (i = 0; i < array.length; i++) {
    bucketIndex = Math.floor(((array[i] - minValue) / exponent) % radix);
    buckets[bucketIndex]++;
  }

  // Compute cumulates
  for (i = 1; i < radix; i++) {
    buckets[i] += buckets[i - 1];
  }

  // Move records
  for (i = array.length - 1; i >= 0; i--) {
    bucketIndex = Math.floor(((array[i] - minValue) / exponent) % radix);
    output[--buckets[bucketIndex]] = array[i];
  }

  // Copy back
  for (i = 0; i < array.length; i++) {
    array[i] = output[i];
  }

  return array;
}
function countingSortByDigit ( array , radix , exponent , minValue ) { var i ; var bucketIndex ; var buckets = new Array ( radix ) ; var output = new Array ( array . length ) ; // Initialize bucket for ( i = 0 ; i < radix ; i ++ ) { buckets [ i ] = 0 ; } // Count frequencies for ( i = 0 ; i < array . length ; i ++ ) { bucketIndex = Math . floor ( ( ( array [ i ] - minValue ) / exponent ) % radix ) ; buckets [ bucketIndex ] ++ ; } // Compute cumulates for ( i = 1 ; i < radix ; i ++ ) { buckets [ i ] += buckets [ i - 1 ] ; } // Move records for ( i = array . length - 1 ; i >= 0 ; i -- ) { bucketIndex = Math . floor ( ( ( array [ i ] - minValue ) / exponent ) % radix ) ; output [ -- buckets [ bucketIndex ] ] = array [ i ] ; } // Copy back for ( i = 0 ; i < array . length ; i ++ ) { array [ i ] = output [ i ] ; } return array ; }
Stable sorts an array by a particular digit using counting sort.
@param {Array} array The array to sort.
@param {number} radix The base/radix to use to sort.
@param {number} exponent The exponent of the significant digit to sort.
@param {number} minValue The minimum value within the array.
@returns The sorted array.
Stable sorts an array by a particular digit using counting sort .