|
| 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); |