-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
288 lines (271 loc) · 10.1 KB
/
Copy pathmain.js
File metadata and controls
288 lines (271 loc) · 10.1 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
let appWidth, appHeight; //variable to maintain the size of the app
let arr = []; //array to store the required elements for the required algorithm
//pageNo: variable to store the current page number
//algoType: variable to store the selected algorithm type, Searching=1, sorting=2
//algoNo: variable to store the selected algorithm within a selected algorithm type
//sortBtn: variable to select the sort button on first page when clicked on it
//searchBtn: variable to select the search button on first page when clicked on it
//treeBtn: variable to select the tree button on first page when clicked on it.
//heading: variable that can be used to manipulate the text of the heading in index page
//subHeading: variable that can be used to manipulate the text of the sub-heading in index page
//firstPageBtns: variable to select all buttons on first page(selects entire div)
//backBtn: variable that will link to previous page
//selectionSort, mergeSort, bubbleSort: selecting the sorting buttons from html and storing inside them
let pageNo, algoType, algoNo, sorted, iterator;
let graphWidth, graphHeight, barWidth, max, graphPadLeft, graphPadTop;
let sortBtn, searchBtn, treeBtn, firstPageBtns, backBtn;
var heading, subheading, firstPageFooter;
var sortPageBtns;
var selectionSort, mergeSort, bubbleSort, btnForArrayGeneration;
function setup() {
appWidth = (windowWidth < 1280) ? 1280 : windowWidth; //minimum width of the app should be 1280 pixels and max should be the windowWidth
appHeight = (windowHeight < 720) ? 720 : windowHeight; //minimum height of the app should be 720 pixels and max should be the windowHeight
createCanvas(appWidth, appHeight);
pageNo = 0;
algoType = 0; //** */
algoNo = 0;
//firstPageBtns = select('#btns'); //storing all btns in of first page in a variable
//firstPageFooter = select('#firstPageFooter');
//sortPageBtns = select('#allSortButtons'); //storing the div containing all sort related buttons
//backBtn = createButton('Back'); //creating a back button
//backBtn.addClass('btn');
//backBtn.addClass('btn-danger');
//backBtn.position(appWidth * 0.02, appHeight * 0.237)
//selectionSort = select('#selectionSort');
//mergeSort = select('#mergeSort');
//bubbleSort = select('#bubbleSort');
//btnForArrayGeneration = select('#btnForArrayGeneration');
//showLandingPage(); //show first page
}
/*
/*------------ Landing Page------------------
function showLandingPage() {
arr = [];
heading = select("#heading"); //Select heading on index page
subHeading = select("#subheading"); //Select sub-heading part on index page
heading.html("Algorithm Visualizer"); //Text of heading on first page
subHeading.html("Watch animations of all Data Structures & Algorithms in Real time"); //text of subheading on first page
firstPageBtns.show(); //show all buttons of first page
firstPageFooter.show(); //show entire footer on first page
backBtn.hide(); //Hiding back button on first page
sortPageBtns.hide() //hiding sort page buttons
sortBtn = select('#sort');
sortBtn.mousePressed(showSortingPage);
}
// ------- End of Landing Page ----------
*/
/*
/*--------------------- Sorting Selected Page ----------------
function showSortingPage() {
// pageNo = 1;
// algoType = 2;
//changing display text of headings
heading.html("Sorting Visualizer"); //Text of heading on first page
subHeading.html("Let's visualize some sorting algorithms!!!"); //text of subheading on first page
pageNo = 1; //Setting variable value to 1 as user chose Sorting
firstPageBtns.hide(); //hiding the buttons of first page
firstPageFooter.hide(); //hiding the footer that was on the landing page.
sortPageBtns.show(); //showing sort page buttons
//adding a back button
backBtn.show()
backBtn.mousePressed(showLandingPage);
//generating and displaying the random array bars
btnForArrayGeneration.mousePressed(createTheArray);
//calling each function when clicked on given algorithm
selectionSort.mousePressed(selectionSortSelected);
mergeSort.mousePressed(mergeSortSelected);
bubbleSort.mousePressed(bubbleSortSelected);
}
/* -------End of Sorting Page --------------
*/
/*
function createTheArray() {
generateArray(10);
}
*/
function draw() {
graphWidth = appWidth * 0.6;
graphHeight = appHeight * 0.6;
barWidth = graphWidth / arr.length;
graphPadLeft = appWidth * 0.2;
graphPadTop = appHeight * 0.3;
clear();
//background(200, 0, 100);
background(247, 185, 199);
fill(0, 200, 200);
//switch case to maintain required pages with pageNo value
switch (pageNo) {
case 0:
rect(graphPadLeft,graphPadTop,400,200);
rect(graphPadLeft+405,graphPadTop,400,200);
fill(0);
text("Searching Algorithms",graphPadLeft,graphPadTop+20);
text("Sorting Algorithms",graphPadLeft+405,graphPadTop+20);
break;
case 1:
if (algoType === 1)
{
rect(graphPadLeft,graphPadTop,400,200);
rect(graphPadLeft+405,graphPadTop,400,200);
fill(0);
text("Linear Search",graphPadLeft,graphPadTop+20);
text("Binary Search",graphPadLeft+405,graphPadTop+20);
}
else if(algoType===2)
{
rect(graphPadLeft,graphPadTop,400,200);
rect(graphPadLeft+405,graphPadTop,400,200);
fill(0);
text("Selection Sort",graphPadLeft,graphPadTop+20);
text("Bubble Sort",graphPadLeft+405,graphPadTop+20);
}
break;
case 2:
if (algoType === 1)
{
if(algoNo===1)
{
}
}
else if(algoType===2)
{
if(algoNo===1)
{
if (sorted === false) {
sorted = selectionSortS(iterator);
iterator = iterator + 1;
}
}
}
break;
}
for (let i = 0; i < arr.length; i++) {
rect(graphPadLeft + i * barWidth, graphPadTop + (max - arr[i]) * graphHeight / max, barWidth, arr[i] * graphHeight / max);
}
}
function windowResized() {
appWidth = (windowWidth < 1280) ? 1280 : windowWidth;
appHeight = (windowHeight < 720) ? 720 : windowHeight;
resizeCanvas(appWidth, appHeight);
}
function mousePressed() {
switch (pageNo) {
case 0:
if(mouseX> graphPadLeft && mouseX< graphPadLeft+400 && mouseY>graphPadTop && mouseY<graphPadTop+200)
{
pageNo++;
algoType=1;
}
else if(mouseX> graphPadLeft+405 && mouseX< graphPadLeft+405+400 && mouseY>graphPadTop && mouseY<graphPadTop+200)
{
pageNo++;
algoType=2;
}
break;
case 1:
if (algoType === 1)
{
if(mouseX> graphPadLeft && mouseX< graphPadLeft+400 && mouseY>graphPadTop && mouseY<graphPadTop+200)
{
pageNo++;
algoNo=1;
}
else if(mouseX> graphPadLeft+405 && mouseX< graphPadLeft+405+400 && mouseY>graphPadTop && mouseY<graphPadTop+200)
{
pageNo++;
algoNo=2;
}
}
else if (algoType === 2)
{
if(mouseX> graphPadLeft && mouseX< graphPadLeft+400 && mouseY>graphPadTop && mouseY<graphPadTop+200)
{
pageNo++;
algoNo=1;
sorted=false;
generateArray(10);
iterator=0;
}
else if(mouseX> graphPadLeft+405 && mouseX< graphPadLeft+405+400 && mouseY>graphPadTop && mouseY<graphPadTop+200)
{
pageNo++;
algoNo=2;
}
}
break;
case 2:
break;
}
}
function generateArray(n) {
arr = [];
max = 0;
for (let i = 0; i < n; i++) {
append(arr, (int)(100 * random()));
if (max < arr[i])
max = arr[i];
}
}
/*
function selectionSortSelected() {
pageNo = 1;
algoType = 2;
algoNo = 1;
heading.html("Selection Sort"); //Text of heading on first page
subHeading.html("It's worst case complexity is O(n<sup>2</sup>). Pretty bad huh?"); //sub-heading text shown upon selecting this sort/search
/* Making button look selected
selectionSort.addClass("active");
bubbleSort.removeClass("active");
mergeSort.removeClass("active");
/* Making button look selected END
}
function mergeSortSelected() {
pageNo = 1;
algoType = 2;
algoNo = 2;
heading.html("Merge Sort"); //Text of heading on first page
subHeading.html("It's worst case complexity is O(n*log(n)). Not Bad!!"); //sub-heading text shown upon selecting this sort/search
/* Making button look selected
selectionSort.removeClass("active");
bubbleSort.removeClass("active");
mergeSort.addClass("active");
/* Making button look selected END
}
function bubbleSortSelected() {
pageNo = 1;
algoType = 2;
algoNo = 3;
heading.html("Bubble Sort"); //Text of heading on first page
subHeading.html("Did you know? It's worst case complexity is O(n<sup>2</sup>)."); //sub-heading text shown upon selecting this sort/search
/* Making button look selected
selectionSort.removeClass("active");
bubbleSort.addClass("active");
mergeSort.removeClass("active");
/* Making button look selected END
}
*/
function selectionSortS(d) {
let y, m, t, n;
myDelay(500);
n = arr.length;
if (d >= n)
return true;
m = d;
for (y = d + 1; y < n; y++) {
if (arr[m] > arr[y])
m = y;
}
if (m != d) {
t = arr[m];
arr[m] = arr[d];
arr[d] = t;
}
if (d < (n - 1))
return false;
else
return true;
}
function myDelay(deltaT) {
let startT = millis();
while (millis() < startT + deltaT);
}