@@ -18,6 +18,10 @@ function ArrayList(){
1818 return array . join ( ) ;
1919 } ;
2020
21+ this . array = function ( ) {
22+ return array ;
23+ } ;
24+
2125 this . bubbleSort = function ( ) {
2226 var length = array . length ;
2327
@@ -300,6 +304,53 @@ function ArrayList(){
300304 }
301305 } ;
302306
307+ this . radixSort = function ( radixBase ) {
308+
309+ var i ,
310+ minValue = this . findMinValue ( ) ,
311+ maxValue = this . findMaxValue ( ) ,
312+ radixBase = radixBase || 10 ;
313+
314+ // Perform counting sort for each significant digit), starting at 1
315+ var significantDigit = 1 ;
316+ while ( ( ( maxValue - minValue ) / significantDigit ) >= 1 ) {
317+ console . log ( 'radix sort for digit ' + significantDigit ) ;
318+ array = countingSortForRadix ( array , radixBase , significantDigit , minValue ) ;
319+ console . log ( array . join ( ) ) ;
320+ significantDigit *= radixBase ;
321+ }
322+ } ;
323+
324+ var countingSortForRadix = function ( array , radixBase , significantDigit , minValue ) {
325+ var i , countsIndex ,
326+ counts = new Array ( radixBase ) ,
327+ aux = new Array ( radixBase ) ;
328+
329+ for ( i = 0 ; i < radixBase ; i ++ ) {
330+ counts [ i ] = 0 ;
331+ }
332+
333+ for ( i = 0 ; i < array . length ; i ++ ) {
334+ countsIndex = Math . floor ( ( ( array [ i ] - minValue ) / significantDigit ) % radixBase ) ;
335+ counts [ countsIndex ] ++ ;
336+ }
337+
338+ for ( i = 1 ; i < radixBase ; i ++ ) {
339+ counts [ i ] += counts [ i - 1 ] ;
340+ }
341+
342+ for ( i = array . length - 1 ; i >= 0 ; i -- ) {
343+ countsIndex = Math . floor ( ( ( array [ i ] - minValue ) / significantDigit ) % radixBase ) ;
344+ aux [ -- counts [ countsIndex ] ] = array [ i ] ;
345+ }
346+
347+ for ( i = 0 ; i < array . length ; i ++ ) {
348+ array [ i ] = aux [ i ] ;
349+ }
350+
351+ return array ;
352+ } ;
353+
303354 this . sequentialSearch = function ( item ) {
304355
305356 for ( var i = 0 ; i < array . length ; i ++ ) {
0 commit comments