|
1 |
| -const { Array2DTracer, Randomize } = require('algorithm-visualizer'); |
| 1 | +// import visualization libraries { |
| 2 | +const { Array1DTracer, Randomize } = require('algorithm-visualizer'); |
| 3 | +// } |
2 | 4 |
|
3 |
| -const maxValue = 9; |
4 |
| -const arrSize = 10; |
| 5 | +// define tracer variables { |
| 6 | +const arrayTracer = new Array1DTracer('Array'); |
| 7 | +const countsTracer = new Array1DTracer('Counts'); |
| 8 | +const sortedArrayTracer = new Array1DTracer('Sorted Array'); |
| 9 | +// } |
5 | 10 |
|
6 |
| -// initialize array values |
7 |
| -const A = new Randomize.Array1D(arrSize, new Randomize.Integer(0, maxValue)).create(); |
8 |
| -const counts = []; |
9 |
| -const sortedA = []; |
10 |
| -for (let i = 0; i <= maxValue; i++) { |
11 |
| - counts[i] = 0; |
12 |
| - if (i < arrSize) sortedA[i] = 0; |
13 |
| -} |
14 |
| -const D = [ |
15 |
| - A, |
16 |
| - counts, |
17 |
| - sortedA, |
18 |
| -]; |
| 11 | +// define input variables |
| 12 | +const N = 20; // the size of an array |
| 13 | +const array = new Randomize.Array1D(N, new Randomize.Integer(0, 9)).create(); |
19 | 14 |
|
20 |
| -const tracer = new Array2DTracer(); |
21 |
| -tracer.set(D).delay(); |
| 15 | +(function main() { |
| 16 | + // find the maximum value that will decide the size of counts array |
| 17 | + const max = Math.max(...array); |
| 18 | + const counts = new Array(max + 1).fill(0); |
| 19 | + // visualize { |
| 20 | + arrayTracer.set(array); |
| 21 | + countsTracer |
| 22 | + .set(counts) |
| 23 | + .delay(); |
| 24 | + // } |
22 | 25 |
|
23 |
| -// set counts values |
24 |
| -for (let i = 0; i < A.length; i++) { |
25 |
| - tracer.select(0, i).delay(); |
26 |
| - counts[A[i]]++; |
27 |
| - tracer.patch(1, A[i], D[1][A[i]]).delay(); |
28 |
| - tracer.deselect(0, i); |
29 |
| - tracer.depatch(1, A[i], D[1][A[i]]).delay(); |
30 |
| -} |
| 26 | + // store counts of each number |
| 27 | + for (let i = 0; i < N; i++) { |
| 28 | + const number = array[i]; |
| 29 | + counts[number]++; |
| 30 | + // visualize { |
| 31 | + arrayTracer.select(i); |
| 32 | + countsTracer |
| 33 | + .patch(number, counts[number]) |
| 34 | + .delay() |
| 35 | + .depatch(number); |
| 36 | + arrayTracer.deselect(i); |
| 37 | + // } |
| 38 | + } |
| 39 | + |
| 40 | + // calculate the prefix sums |
| 41 | + for (let i = 1; i <= max; i++) { |
| 42 | + counts[i] += counts[i - 1]; |
| 43 | + // visualize { |
| 44 | + countsTracer |
| 45 | + .select(i - 1) |
| 46 | + .patch(i, counts[i]) |
| 47 | + .delay() |
| 48 | + .depatch(i) |
| 49 | + .deselect(i - 1); |
| 50 | + // } |
| 51 | + } |
31 | 52 |
|
32 |
| -// sort |
33 |
| -let i = 0; |
34 |
| -for (let j = 0; j <= maxValue; j++) { |
35 |
| - while (counts[j] > 0) { |
36 |
| - tracer.select(1, j).delay(); |
37 |
| - sortedA[i] = j; |
38 |
| - counts[j]--; |
39 |
| - tracer.patch(1, j, D[1][j]); |
40 |
| - tracer.patch(2, i, D[2][i]).delay(); |
41 |
| - tracer.deselect(1, j); |
42 |
| - tracer.depatch(1, j, D[1][j]); |
43 |
| - tracer.depatch(2, i, D[2][i]).delay(); |
44 |
| - i++; |
| 53 | + // create a sorted array based on the prefix sums |
| 54 | + const sortedArray = new Array(N); |
| 55 | + // visualize { |
| 56 | + sortedArrayTracer.set(sortedArray); |
| 57 | + // } |
| 58 | + for (let i = N - 1; i >= 0; i--) { |
| 59 | + const number = array[i]; |
| 60 | + const count = counts[number]; |
| 61 | + sortedArray[count - 1] = number; |
| 62 | + // visualize { |
| 63 | + arrayTracer.select(i); |
| 64 | + countsTracer.select(number); |
| 65 | + sortedArrayTracer |
| 66 | + .patch(count - 1, sortedArray[count - 1]) |
| 67 | + .delay() |
| 68 | + .depatch(count - 1); |
| 69 | + countsTracer.deselect(number); |
| 70 | + arrayTracer.deselect(i); |
| 71 | + // } |
| 72 | + counts[number]--; |
45 | 73 | }
|
46 |
| -} |
| 74 | +})(); |
0 commit comments