Skip to content

Commit eeb1ec7

Browse files
committed
Updated Algorithm with helper functions
Moved to a system to use functions to handle incrementing counters etc...
1 parent 33291cb commit eeb1ec7

1 file changed

Lines changed: 28 additions & 25 deletions

File tree

Algorithm-Visualiser.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@ class algorithmVisualiser{
22
configVariables(){ // Bodge for ES6 not supporting Public Field Declarations
33
self.dataSet = {
44
data: [],
5-
comp: [],
5+
compIndicator: [],
66
compCount: 0,
7-
swap: [],
7+
swapIndicator: [],
88
swapCount: 0,
9-
iteration: 0
9+
iterationCount: 0,
10+
iteration: () => { self.dataSet.iterationCount++; self.dataSet.swapIndicator = []; self.dataSet.compIndicator = []; },
11+
swap: (x,y) => {
12+
[ self.dataSet.data[x], self.dataSet.data[y] ] = [ self.dataSet.data[y], self.dataSet.data[x] ];
13+
self.dataSet.swapIndicator = [x, y];
14+
self.dataSet.swapCount++;
15+
},
16+
lessthan: (x,y) => { self.dataSet.compIndicator = [x, y]; self.dataSet.compCount++; return self.dataSet.data[x] < self.dataSet.data[y]},
17+
greaterthan: (x,y) => { self.dataSet.compIndicator = [x, y]; self.dataSet.compCount++; return self.dataSet.data[x] > self.dataSet.data[y]},
1018
};
1119
self.options = {
1220
size: 128,
@@ -18,7 +26,8 @@ class algorithmVisualiser{
1826
lineColor: "#0000FF",
1927
compColor: "#FF0000",
2028
swapColor: "#00FF00",
21-
lineWidth: 2.5
29+
lineWidth: 2.5,
30+
fontSize: 15,
2231
};
2332
}
2433
constructor(canvas, opts = {}) {
@@ -42,6 +51,7 @@ class algorithmVisualiser{
4251
advFolder.addColor(self.options, 'compColor');
4352
advFolder.addColor(self.options, 'swapColor');
4453
advFolder.add(self.options, 'lineWidth', 0, 15);
54+
advFolder.add(self.options, 'fontSize', 0, 72);
4555

4656
if(self.options.fps != false && self.options.gui){
4757
self.options.fps = new Stats();
@@ -75,7 +85,7 @@ class algorithmVisualiser{
7585

7686
reset(){
7787
self.structures.find(x => x.name === self.options.structure).func();
78-
self.dataSet.iteration = 0;
88+
self.dataSet.iterationCount = 0;
7989
self.dataSet.swapCount = 0;
8090
self.dataSet.compCount = 0;
8191
}
@@ -94,12 +104,12 @@ class algorithmVisualiser{
94104
ctx.lineWidth = lineWidth;
95105
ctx.beginPath();
96106

97-
if(self.dataSet.comp.includes(i)){ // If the line is currently in a comparison, draw it in that colour
107+
if(self.dataSet.compIndicator.includes(i)){ // If the line is currently in a comparison, draw it in that colour
98108
ctx.lineWidth = lineWidth*3;
99109
ctx.strokeStyle = self.options.compColor;
100110
}
101111

102-
if(self.dataSet.swap.includes(i)){ // If the line is currently in a swap, draw it in that colour
112+
if(self.dataSet.swapIndicator.includes(i)){ // If the line is currently in a swap, draw it in that colour
103113
ctx.lineWidth = lineWidth*3;
104114
ctx.strokeStyle = self.options.swapColor;
105115
}
@@ -110,9 +120,9 @@ class algorithmVisualiser{
110120
ctx.stroke();
111121
}
112122

113-
ctx.font = "15px Arial";
114-
var textStr = `${self.options.algorithm} Iteration: ${self.dataSet.iteration} Comparisons: ${self.dataSet.compCount} Swaps: ${self.dataSet.swapCount}`;
115-
ctx.fillText(textStr, 10, 25);
123+
ctx.font = self.options.fontSize + 'px Arial';
124+
var textStr = `${self.options.algorithm} Iteration: ${self.dataSet.iterationCount} Comparisons: ${self.dataSet.compCount} Swaps: ${self.dataSet.swapCount}`;
125+
ctx.fillText(textStr, 10, self.options.fontSize + 10);
116126

117127
if(self.options.fps != false){self.options.fps.end();}
118128
window.requestAnimationFrame( () => self.draw() );
@@ -151,31 +161,24 @@ class algorithmVisualiser{
151161
self.algorithms = [
152162

153163
{name:"Bubble Sort",algorithm: async ()=>{
154-
for (let i = ( self.dataSet.data.length - 1); i >= 0; i--){
155-
for (let j = ( self.dataSet.data.length - i); j > 0; j--){
156-
self.dataSet.iteration++;
157-
self.dataSet.compCount++;
158-
self.dataSet.swap = [];
159-
self.dataSet.comp = [j-1, j];
160-
if (self.dataSet.data[j] < self.dataSet.data[j - 1]){
161-
[ self.dataSet.data[j-1], self.dataSet.data[j] ] = [ self.dataSet.data[j], self.dataSet.data[j-1] ];
162-
self.dataSet.swap = [j-1, j];
163-
self.dataSet.swapCount++;
164+
for (var i = ( self.dataSet.data.length - 1); i >= 0; i--){
165+
for (var j = ( self.dataSet.data.length - i); j > 0; j--){
166+
self.dataSet.iteration(); // Iterate counter etc...
167+
if (self.dataSet.lessthan(j, j-1)){
168+
self.dataSet.swap(j, j-1);
164169
}
165170
await self.wait(self.options.delay); // Delay before next iteration
166171
}
167172
}
168-
self.dataSet.swap = []; // Finished Empty active data
169-
self.dataSet.comp = []; // Finished Empty active data
173+
self.dataSet.swapIndicator = []; // Finished Empty active data
174+
self.dataSet.swapIndicator =[]; // Finished Empty active data
170175
}},
171176

172177
{name:"Quick Sort",algorithm: async ()=>{
173178

174179
}},
175180

176-
{name:"Reverse Order",algorithm: async ()=>{
177-
178-
}}
181+
179182

180183
];
181184
}

0 commit comments

Comments
 (0)