-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
40 lines (34 loc) · 879 Bytes
/
stack.js
File metadata and controls
40 lines (34 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* In-place, stable implementation of radix-sort for a stack.
*
* @param {function} lsb takes two arguments `(key, shift)` and returns the least
* significant bit of `key` shifted to the right by `shift` places.
* @param {array} main The input stack..
* @param {int} hi An empty stack.
* @param {int} lo An empty stack.
* @param {int} si The number of places the keys are shifted before
* computing the LSB.
* @param {int} sj `sj-si` gives the number of symbols considered by the
* sorting algorithm.
*
*/
export function stack(lsb, main, hi, lo, si, sj) {
if (si >= sj) {
return;
}
while (!main.empty()) {
const key = main.pop();
if (lsb(key, si) === 0) {
lo.push(key);
} else {
hi.push(key);
}
}
while (!hi.empty()) {
main.push(hi.pop());
}
while (!lo.empty()) {
main.push(lo.pop());
}
stack(lsb, main, hi, lo, si + 1, sj);
}