Skip to content

Commit 14bf689

Browse files
drafts
1 parent 006f0bb commit 14bf689

File tree

6 files changed

+176
-0
lines changed

6 files changed

+176
-0
lines changed

.jshintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"esnext" : true
3+
}

js/src/dummy.js

Whitespace-only changes.

js/src/inplace.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
let _inplace = function ( split ) {
3+
4+
/**
5+
* In place implementation of radix sort. NOT STABLE.
6+
*
7+
* @param {function} msb takes two arguments `(key, shift)` and returns the most
8+
* significant bit of `key` shifted to the left 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 {int} si The number of places the keys are shifted before
13+
* computing the LSB.
14+
* @param {int} sj `sj-si` gives the number of symbols considered by the
15+
* sorting algorithm.
16+
*/
17+
18+
let inplace = function ( msb , a , ai , aj , si , sj ) {
19+
20+
if ( si >= sj ) return ;
21+
22+
let pivot = split( x => msb( x , si ) , a , ai , aj ) ;
23+
24+
// sort according to remaining symbols
25+
26+
inplace( msb , a , ai , pivot , si + 1 , sj ) ;
27+
inplace( msb , a , pivot + 1 , aj , si + 1 , sj ) ;
28+
29+
} ;
30+
31+
return inplace ;
32+
33+
} ;
34+
35+
exports._inplace = _inplace ;

js/src/nodes.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
3+
/**
4+
* In-place, stable implementation of radix-sort for singly linked lists.
5+
*
6+
* @param {function} lsb takes two arguments `(key, shift)` and returns the least
7+
* significant bit of `key` shifted to the right by `shift` places.
8+
* @param {array} head The first node of the input list.
9+
* @param {int} hi A next pointer ({ next : null }).
10+
* @param {int} lo A next pointer ({ next : null }).
11+
* @param {int} si The number of places the keys are shifted before
12+
* computing the LSB.
13+
* @param {int} sj `sj-si` gives the number of symbols considered by the
14+
* sorting algorithm.
15+
*
16+
*/
17+
18+
let nodes = function ( lsb , head , hi , lo , si , sj ) {
19+
20+
if ( si >= sj ) return head ;
21+
22+
let _lo = lo , _hi = hi ;
23+
24+
while ( head !== null ) {
25+
26+
if ( lsb ( head.value , si ) === 0 ) _lo = _lo.next = head ;
27+
28+
else _hi = _hi.next = head ;
29+
30+
head = head.next ;
31+
32+
}
33+
34+
if ( lo.next === null ) {
35+
36+
head = hi.next ;
37+
hi.next = null ;
38+
39+
}
40+
41+
else {
42+
43+
_lo.next = hi.next ;
44+
head = lo.next ;
45+
lo.next = null ;
46+
hi.next = null ;
47+
48+
}
49+
50+
return nodes( lsb , head , hi , lo , si + 1 , sj ) ;
51+
52+
} ;
53+
54+
exports.nodes = nodes ;

js/src/stable.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 ;

js/src/stack.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
3+
/**
4+
* In-place, stable implementation of radix-sort for a stack.
5+
*
6+
* @param {function} lsb takes two arguments `(key, shift)` and returns the least
7+
* significant bit of `key` shifted to the right by `shift` places.
8+
* @param {array} main The input stack..
9+
* @param {int} hi An empty stack.
10+
* @param {int} lo An empty stack.
11+
* @param {int} si The number of places the keys are shifted before
12+
* computing the LSB.
13+
* @param {int} sj `sj-si` gives the number of symbols considered by the
14+
* sorting algorithm.
15+
*
16+
*/
17+
18+
let stack = function ( lsb , main , hi , lo , si , sj ) {
19+
20+
if ( si >= sj ) return ;
21+
22+
while ( !main.empty( ) ) {
23+
24+
let key = main.pop( ) ;
25+
26+
if ( lsb ( key , si ) === 0 ) lo.push( key ) ;
27+
28+
else hi.push( key ) ;
29+
30+
}
31+
32+
while ( !hi.empty( ) ) main.push( hi.pop( ) ) ;
33+
34+
while ( !lo.empty( ) ) main.push( lo.pop( ) ) ;
35+
36+
stack( lsb , main , hi , lo , si + 1 , sj ) ;
37+
38+
} ;
39+
40+
exports.stack = stack ;

0 commit comments

Comments
 (0)