|
| 1 | + |
| 2 | +let _stable = function ( split ) { |
| 3 | + |
| 4 | + /** |
| 5 | + * In place implementation of radix sort. NOT STABLE. |
| 6 | + * |
| 7 | + * @param {function} lsb takes two arguments `(key, shift)` and returns the least |
| 8 | + * significant bit of `key` shifted to the right by `shift` places. |
| 9 | + * @param {array} a The input array. |
| 10 | + * @param {int} ai Input array left offset. |
| 11 | + * @param {int} aj Input array right offset. |
| 12 | + * @param {array} b The counting array. |
| 13 | + * @param {int} bi Counting array left offset. |
| 14 | + * @param {int} bj Counting array right offset. |
| 15 | + * @param {array} c The output array. |
| 16 | + * @param {int} ci Output array left offset. |
| 17 | + * @param {int} cj Output array right offset. |
| 18 | + * @param {int} si The number of places the keys are shifted before |
| 19 | + * computing the LSB. |
| 20 | + * @param {int} sj `sj-si` gives the number of symbols considered by the |
| 21 | + * sorting algorithm. |
| 22 | + */ |
| 23 | + |
| 24 | + let stable = function ( lsb , a , ai , aj , b , bi , bj , c , ci , cj , si , sj ) { |
| 25 | + |
| 26 | + if ( si >= sj ) return ; |
| 27 | + |
| 28 | + split( x => lsb( x , si ) , a , ai , aj , b , bi , bj , c , ci ) ; |
| 29 | + |
| 30 | + // reset the counting buffer |
| 31 | + |
| 32 | + for ( let bk = bi ; bk < bj ; ++bk ) b[bk] = 0 ; |
| 33 | + |
| 34 | + // sort according to remaining symbols |
| 35 | + |
| 36 | + stable( lsb , c , ci , cj , b , bi , bj , a , ai , aj , si + 1 , sj ) ; |
| 37 | + |
| 38 | + } ; |
| 39 | + |
| 40 | + return stable ; |
| 41 | + |
| 42 | +} ; |
| 43 | + |
| 44 | +exports._stable = _stable ; |
0 commit comments