forked from ObiWanKeoni/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
32 lines (30 loc) · 919 Bytes
/
Copy pathcode.js
File metadata and controls
32 lines (30 loc) · 919 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
import { Array1DTracer, ChartTracer, LogTracer, Randomize } from 'algorithm-visualizer';
const chart = new ChartTracer();
const tracer = new Array1DTracer().chart(chart);
const logger = new LogTracer();
const D = new Randomize.Array1D(15).create();
tracer.set(D).delay();
logger.print(`original array = [${D.join(', ')}]`);
for (let i = 0; i < D.length - 1; i++) {
let minJ = i;
tracer.select(i).delay();
for (let j = i + 1; j < D.length; j++) {
tracer.select(j).delay();
if (D[j] < D[minJ]) {
minJ = j;
tracer.patch(j).delay();
tracer.depatch(j);
}
tracer.deselect(j);
}
if (minJ !== i) {
logger.print(`swap ${D[i]} and ${D[minJ]}`);
const temp = D[i];
D[i] = D[minJ];
D[minJ] = temp;
tracer.patch(i, D[i]).patch(minJ, D[minJ]).delay();
tracer.depatch(i).depatch(minJ);
}
tracer.deselect(i);
}
logger.print(`sorted array = [${D.join(', ')}]`);