|
1 | | -const { LogTracer, VerticalLayout } = require('../dist/algorithm-visualizer'); |
| 1 | +// import visualization libraries { |
| 2 | +const { Tracer, Array1DTracer, Array2DTracer, ChartTracer, Randomize, Layout, VerticalLayout } = require('..'); |
| 3 | +// } |
2 | 4 |
|
3 | | -const logTracer = new LogTracer(); |
4 | | -new VerticalLayout([logTracer]); |
| 5 | +// define tracer variables { |
| 6 | +const chartTracer = new ChartTracer('Chart'); |
| 7 | +const arrayTracer = new Array1DTracer('Array'); |
| 8 | +const bucketsTracer = new Array2DTracer('Buckets'); |
| 9 | +Layout.setRoot(new VerticalLayout([chartTracer, arrayTracer, bucketsTracer])); |
| 10 | +// } |
5 | 11 |
|
6 | | -logTracer.set('haahahahaa: '); |
| 12 | +// define input variables |
| 13 | +const N = 25; // the size of an array |
| 14 | +const K = 5; // the number of buckets |
| 15 | +const array = new Randomize.Array1D(N, new Randomize.Integer(0, 999)).create(); |
7 | 16 |
|
8 | | -for (let i = 0; i < 3; i++) |
9 | | - logTracer.print('tt ee ss tt').delay(); |
| 17 | +(function main() { |
| 18 | + // create K buckets |
| 19 | + const buckets = [...new Array(K)].map(() => []); |
| 20 | + // visualize { |
| 21 | + arrayTracer.chart(chartTracer); |
| 22 | + arrayTracer.set(array); |
| 23 | + bucketsTracer.set(buckets); |
| 24 | + Tracer.delay(); |
| 25 | + // } |
| 26 | + |
| 27 | + // find the maximum value that will be used for distribution |
| 28 | + const max = Math.max(...array); |
| 29 | + |
| 30 | + // distribute the elements into the buckets |
| 31 | + for (let i = 0; i < N; i++) { |
| 32 | + const number = array[i]; |
| 33 | + const bucketIndex = Math.floor(number / (max + 1) * K); |
| 34 | + const bucket = buckets[bucketIndex]; |
| 35 | + bucket.push(number); |
| 36 | + // visualize { |
| 37 | + arrayTracer.select(i); |
| 38 | + bucketsTracer.patch(bucketIndex, bucket.length - 1, number); |
| 39 | + Tracer.delay(); |
| 40 | + bucketsTracer.depatch(bucketIndex, bucket.length - 1); |
| 41 | + // } |
| 42 | + |
| 43 | + // insertion sort within the bucket |
| 44 | + let j = bucket.length - 1; |
| 45 | + while (j > 0 && bucket[j - 1] > bucket[j]) { |
| 46 | + const temp = bucket[j - 1]; |
| 47 | + bucket[j - 1] = bucket[j]; |
| 48 | + bucket[j] = temp; |
| 49 | + // visualize { |
| 50 | + bucketsTracer.patch(bucketIndex, j - 1, bucket[j - 1]); |
| 51 | + bucketsTracer.patch(bucketIndex, j, bucket[j]); |
| 52 | + Tracer.delay(); |
| 53 | + bucketsTracer.depatch(bucketIndex, j - 1); |
| 54 | + bucketsTracer.depatch(bucketIndex, j); |
| 55 | + // } |
| 56 | + j--; |
| 57 | + } |
| 58 | + // visualize { |
| 59 | + arrayTracer.deselect(i); |
| 60 | + // } |
| 61 | + } |
| 62 | + |
| 63 | + // concatenate the buckets back into the array |
| 64 | + let i = 0; |
| 65 | + for (let bucketIndex = 0; bucketIndex < K; bucketIndex++) { |
| 66 | + const bucket = buckets[bucketIndex]; |
| 67 | + for (let j = 0; j < bucket.length; j++) { |
| 68 | + array[i] = bucket[j]; |
| 69 | + // visualize { |
| 70 | + arrayTracer.patch(i, array[i]); |
| 71 | + bucketsTracer.select(bucketIndex, j); |
| 72 | + Tracer.delay(); |
| 73 | + bucketsTracer.deselect(bucketIndex, j); |
| 74 | + arrayTracer.depatch(i); |
| 75 | + // } |
| 76 | + i++; |
| 77 | + } |
| 78 | + } |
| 79 | +})(); |
0 commit comments