Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8787d14

Browse files
committed
Merge pull request mgechev#84 from lekkas/radix-sort
Add radix sort algorithm
2 parents f4b8566 + 0f4d8cb commit 8787d14

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

src/sorting/radixsort.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
(function (exports) {
2+
'use strict';
3+
4+
var radixSort = (function() {
5+
6+
/**
7+
* Returns the digit of a number that is 'lsdOffset'
8+
* places from the least significant digit.
9+
*
10+
* @private
11+
* @param {Number} number Number
12+
* @param {Number} lsdOffset Offset of the digit to return, counting
13+
* from the position of the least significant digit (e.g. lsdOffset = 0
14+
* will return the least significant digit itself)
15+
* @return {String} digit The specified number digit. Returns 'undefined'
16+
* if lsdOffset is bigger or equal to the number of digits of the 'number'
17+
* argument.
18+
*/
19+
var getDigit = function(number, lsdOffset) {
20+
var numStr = ''+number;
21+
var size = numStr.length;
22+
var digit;
23+
24+
if (lsdOffset >= 0 && lsdOffset < size) {
25+
digit = numStr[size - 1 - lsdOffset];
26+
}
27+
28+
return digit;
29+
};
30+
31+
/**
32+
* Least significant digit (LSD) Radix sort. A non-comparative,
33+
* stable integer sorting algorithm.<br><br>
34+
* Worst-case time complexity is O(N K) for N keys with K being
35+
* the average key length, measured in number of digits.
36+
*
37+
* @example
38+
* var sort = require('path-to-algorithms/src/' +
39+
* 'sorting/radixsort').radixSort;
40+
* console.log(sort([2, 5, 1, 3, 4])); // [ 1, 2, 3, 4, 5 ]
41+
*
42+
* @public
43+
* @module sorting/radixsort
44+
* @param {Array} array Input integer array
45+
* @return {Array} Sorted array
46+
*/
47+
return function(array) {
48+
var size = array.length;
49+
var R = 10; /* Alphabet size ([0-9] for integers) */
50+
var count;
51+
var digit;
52+
var i, j;
53+
54+
/* Find maximum key size */
55+
var maxKeySize = ('' + array[0]).length;
56+
for (i = 1; i < size; i += 1) {
57+
var numStr = '' + array[i];
58+
if (numStr.length > maxKeySize) {
59+
maxKeySize = numStr.length;
60+
}
61+
}
62+
63+
for (i = 0; i < maxKeySize; i += 1) {
64+
/* Initialize count */
65+
count = [];
66+
for (j = 0; j < R; j += 1) {
67+
count[j] = 0;
68+
}
69+
70+
/* Count frequency of each array element */
71+
for (j = 0; j < array.length; j += 1) {
72+
digit = getDigit(array[j], i) || 0;
73+
count[digit] += 1;
74+
}
75+
76+
/* Compute cumulates */
77+
for (j = 1; j < R; j += 1) {
78+
count[j] += count[j - 1];
79+
}
80+
81+
/* Move elements to auxilary array */
82+
var aux = [];
83+
for (j = array.length - 1; j >= 0; j -= 1) {
84+
digit = getDigit(array[j], i) || 0;
85+
count[digit] -= 1;
86+
aux[count[digit]] = array[j];
87+
}
88+
89+
/* Copy elements back from auxilary array */
90+
for (j = 0; j < array.length; j += 1) {
91+
array[j] = aux[j];
92+
}
93+
}
94+
return array;
95+
};
96+
})();
97+
98+
exports.radixSort = radixSort;
99+
100+
})(typeof window === 'undefined' ? module.exports : window);

test/sorting/radixsort.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var rx =
2+
require('../../src/sorting/radixsort.js').radixSort;
3+
4+
describe('radixsort', function () {
5+
'use strict';
6+
7+
it('should sort the empty array', function () {
8+
expect(rx([])).toEqual([]);
9+
});
10+
11+
it('should return array with the same count of elements', function () {
12+
expect(rx([2, 3, 4]).length).toBe(3);
13+
});
14+
15+
it('should sort the given array in ascending order', function () {
16+
expect(rx([42, 3, 10])).toEqual([3, 10, 42]);
17+
});
18+
});

0 commit comments

Comments
 (0)