Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit766c111

Browse files
committed
Merge pull requestmgechev#86 from lekkas/radix-sort
Add radix sort algorithm
2 parents128ce56 +795a470 commit766c111

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+
varradixSort=(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+
vargetDigit=function(number,lsdOffset){
20+
varsize=number.toString().length;
21+
vardigit;
22+
23+
if(lsdOffset>=0&&lsdOffset<size){
24+
digit=number.toString()[size-1-lsdOffset];
25+
}
26+
27+
returndigit;
28+
};
29+
30+
/**
31+
* Least significant digit (LSD) Radix sort. A non-comparative,
32+
* stable integer sorting algorithm.<br><br>
33+
* Worst-case time complexity is O(N K) for N keys with K being
34+
* the average key length, measured in number of digits.
35+
*
36+
*@example
37+
* var sort = require('path-to-algorithms/src/' +
38+
* 'sorting/radixsort').radixSort;
39+
* console.log(sort([2, 5, 1, 3, 4])); // [ 1, 2, 3, 4, 5 ]
40+
*
41+
*@public
42+
*@module sorting/radixsort
43+
*@param {Array} array Input integer array
44+
*@return {Array} Sorted array
45+
*/
46+
returnfunction(array){
47+
varsize=array.length;
48+
varR=10;/* Alphabet size ([0-9] for integers) */
49+
varcount;
50+
vardigit;
51+
vari;
52+
varj;
53+
54+
/* Find maximum key size */
55+
varmaxKeySize=(array[0]||'').toString().length;
56+
for(i=1;i<size;i+=1){
57+
varnumStr=array[i].toString();
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+
varaux=[];
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+
returnarray;
95+
};
96+
})();
97+
98+
exports.radixSort=radixSort;
99+
100+
})(typeofwindow==='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+
varrx=
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)

[8]ページ先頭

©2009-2025 Movatter.jp