/
bars.js
176 lines (157 loc) · 4.86 KB
/
bars.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import createDataSet, {
createDataSetRange,
createUnshuffledDataSetWithComparison,
createReverseDataSet,
checkArray,
compareArrays,
} from "./dataset.js";
import { insertionSortDraw } from "./sortingalgos/insertionsort.js";
import { mergeSortDraw } from "./sortingalgos/mergesort.js";
let maxValue;
let numberOfElements = 200;
let mergeSortDataSet;
let insertionSortDataSet;
let comparison;
const drawDelay = 10;
let mergeHighlight = -1;
let mergeKey = 0;
let insertionHighlight = -1;
let insertionKey = 0;
let highlightColor = "#FF0000C8";
let sortData;
let overlay = true;
window.setup = () => {
createCanvas(innerWidth, innerHeight).position(0, 0);
noLoop();
maxValue = numberOfElements;
//dataset = createDataSetRange(numberOfElements, 0, maxValue);
//[mergeSortDataSet, comparison] = createUnshuffledDataSetWithComparison(numberOfElements); //Sorted order
//[mergeSortDataSet, comparison] = createReverseDataSet(numberOfElements); //Reverse order
[mergeSortDataSet, comparison] = createDataSet(numberOfElements); // Random order
insertionSortDataSet = [...mergeSortDataSet];
console.log(mergeSortDataSet);
drawArray(mergeSortDataSet);
let p1 = new Promise(async (resolve, reject) => {
console.log("Beginning merge sort");
//await insertionSortDraw(dataset, drawArray);
await mergeSortDraw(
mergeSortDataSet,
0,
mergeSortDataSet.length - 1,
drawArray
);
resolve();
});
let p2 = new Promise(async (resolve, reject) => {
console.log("Beginning insertion sort");
//await insertionSortDraw(dataset, drawArray);
await insertionSortDraw(insertionSortDataSet, drawArray);
resolve();
});
Promise.all([p1, p2]).then(() => {
console.log("Sort done");
//const correct = checkArray(dataset);
const correct = comparison
? compareArrays(mergeSortDataSet, comparison)
: checkArray(mergeSortDataSet);
console.log(`Result: ${correct}`);
//console.log(dataset);
});
};
function getColor(number, index, dataSet) {
if (comparison && comparison[index] == dataSet[index])
return color(0, 255, 0);
const ratio = (number / maxValue) * 45 + 55;
return color(100, ratio, 255);
}
window.draw = () => {
background("black");
//Calculates width of one bar
const barWidth = width / mergeSortDataSet.length;
if (barWidth > 10) {
stroke("white");
strokeWeight(barWidth * 0.1);
} else noStroke();
//Draw mergeSort data
for (let i = 0; i < mergeSortDataSet.length; i++) {
const mergeColor = getColor(mergeSortDataSet[i], i, mergeSortDataSet);
fill(mergeColor);
const value = mergeSortDataSet[i];
const barHeightMerge = (value / maxValue) * (height / 2);
rect(i * barWidth, height / 2, barWidth, -barHeightMerge);
}
//Draw insertion sort data
for (let i = 0; i < insertionSortDataSet.length; i++) {
//Draw InsertionSort data
const insertinoColor = getColor(
insertionSortDataSet[i],
i,
insertionSortDataSet
);
fill(insertinoColor);
const value = insertionSortDataSet[i];
const barHeightInsertion = (value / maxValue) * (height / 2);
rect(i * barWidth, height, barWidth, -barHeightInsertion);
}
//Merge Highlight
fill(highlightColor);
const mergeBarHeight = (mergeKey / maxValue) * (height / 2);
rect(mergeHighlight * barWidth, height / 2, barWidth, -mergeBarHeight);
//Insertion Highlight
fill(highlightColor);
const insertionBarHeight = (insertionKey / maxValue) * (height / 2);
rect(insertionHighlight * barWidth, height, barWidth, -insertionBarHeight);
//Merge sort bounds
if (sortData && sortData.mergeSortBounds) {
const leftBound = sortData.mergeSortBounds[0];
const rightBound = sortData.mergeSortBounds[1];
const middle = sortData.mergeSortBounds[2];
const barHeight = height / 2;
fill("white");
rect(
leftBound * barWidth,
height / 2,
Math.max(1, barWidth * 0.3),
-barHeight
);
rect(
(rightBound + (1 - 0.3)) * barWidth,
height / 2,
Math.max(1, barWidth * 0.3),
-barHeight
);
rect(
middle * barWidth - 0.15 * barWidth,
height / 2,
Math.max(1, barWidth * 0.3),
-barHeight
);
}
if (overlay) drawOverlay();
};
function drawOverlay() {
rect(0, height / 2, width, 2);
textAlign(LEFT, TOP);
textSize(16);
text("Merge Sort", 10, 0 + 10);
text("InsertionSort", 10, height / 2 + 10);
}
async function drawArray({
mergeHighlightIndex,
mergeHighlightKey,
insertionHighlightKey,
insertionHighlightIndex,
sortData: data,
}) {
mergeHighlight = mergeHighlightIndex ?? mergeHighlight;
mergeKey = mergeHighlightKey ?? mergeKey;
insertionHighlight = insertionHighlightIndex ?? insertionHighlight;
insertionKey = insertionHighlightKey ?? insertionKey;
sortData = data ?? sortData;
redraw();
await new Promise((res, rej) => {
setTimeout(() => {
res();
}, drawDelay);
});
}