From 4405e249202af3197183e79c297bca12bbbfc415 Mon Sep 17 00:00:00 2001 From: Minko Gechev Date: Wed, 2 Sep 2015 15:04:08 +0300 Subject: [PATCH] Revert "Add radix sort algorithm" --- src/sorting/radixsort.js | 100 --------------------------------- test/sorting/radixsort.spec.js | 18 ------ 2 files changed, 118 deletions(-) delete mode 100644 src/sorting/radixsort.js delete mode 100644 test/sorting/radixsort.spec.js diff --git a/src/sorting/radixsort.js b/src/sorting/radixsort.js deleted file mode 100644 index 94d3bfbd..00000000 --- a/src/sorting/radixsort.js +++ /dev/null @@ -1,100 +0,0 @@ -(function (exports) { - 'use strict'; - - var radixSort = (function() { - - /** - * Returns the digit of a number that is 'lsdOffset' - * places from the least significant digit. - * - * @private - * @param {Number} number Number - * @param {Number} lsdOffset Offset of the digit to return, counting - * from the position of the least significant digit (e.g. lsdOffset = 0 - * will return the least significant digit itself) - * @return {String} digit The specified number digit. Returns 'undefined' - * if lsdOffset is bigger or equal to the number of digits of the 'number' - * argument. - */ - var getDigit = function(number, lsdOffset) { - var numStr = ''+number; - var size = numStr.length; - var digit; - - if (lsdOffset >= 0 && lsdOffset < size) { - digit = numStr[size - 1 - lsdOffset]; - } - - return digit; - }; - - /** - * Least significant digit (LSD) Radix sort. A non-comparative, - * stable integer sorting algorithm.

- * Worst-case time complexity is O(N K) for N keys with K being - * the average key length, measured in number of digits. - * - * @example - * var sort = require('path-to-algorithms/src/' + - * 'sorting/radixsort').radixSort; - * console.log(sort([2, 5, 1, 3, 4])); // [ 1, 2, 3, 4, 5 ] - * - * @public - * @module sorting/radixsort - * @param {Array} array Input integer array - * @return {Array} Sorted array - */ - return function(array) { - var size = array.length; - var R = 10; /* Alphabet size ([0-9] for integers) */ - var count; - var digit; - var i, j; - - /* Find maximum key size */ - var maxKeySize = ('' + array[0]).length; - for (i = 1; i < size; i += 1) { - var numStr = '' + array[i]; - if (numStr.length > maxKeySize) { - maxKeySize = numStr.length; - } - } - - for (i = 0; i < maxKeySize; i += 1) { - /* Initialize count */ - count = []; - for (j = 0; j < R; j += 1) { - count[j] = 0; - } - - /* Count frequency of each array element */ - for (j = 0; j < array.length; j += 1) { - digit = getDigit(array[j], i) || 0; - count[digit] += 1; - } - - /* Compute cumulates */ - for (j = 1; j < R; j += 1) { - count[j] += count[j - 1]; - } - - /* Move elements to auxilary array */ - var aux = []; - for (j = array.length - 1; j >= 0; j -= 1) { - digit = getDigit(array[j], i) || 0; - count[digit] -= 1; - aux[count[digit]] = array[j]; - } - - /* Copy elements back from auxilary array */ - for (j = 0; j < array.length; j += 1) { - array[j] = aux[j]; - } - } - return array; - }; - })(); - - exports.radixSort = radixSort; - -})(typeof window === 'undefined' ? module.exports : window); diff --git a/test/sorting/radixsort.spec.js b/test/sorting/radixsort.spec.js deleted file mode 100644 index 4197be94..00000000 --- a/test/sorting/radixsort.spec.js +++ /dev/null @@ -1,18 +0,0 @@ -var rx = - require('../../src/sorting/radixsort.js').radixSort; - -describe('radixsort', function () { - 'use strict'; - - it('should sort the empty array', function () { - expect(rx([])).toEqual([]); - }); - - it('should return array with the same count of elements', function () { - expect(rx([2, 3, 4]).length).toBe(3); - }); - - it('should sort the given array in ascending order', function () { - expect(rx([42, 3, 10])).toEqual([3, 10, 42]); - }); -});