-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
254 lines (210 loc) · 5.44 KB
/
sketch.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
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
//global variables
var arr = [];
let w = 0; //width of each rectangular bar
let bool = []; //to color a specific bar
let QSB; //quicksort button object
let BSB; //bubble sort button object
let ISB; //insertion sort button object
let SSB; //selection sort button object
let running = -1;
let r,g,b;
function setup() {
w = windowWidth / 100;
createCanvas(windowWidth, windowHeight);
frameRate(60);
//creating button objects
QSB = createButton("QUICK SORT");
BSB = createButton("BUBBLE SORT");
ISB = createButton("INSERTION SORT");
SSB = createButton("SELECTION SORT");
//button styling
QSB.style('border-color:#00ffff');
QSB.style('color:black ');
QSB.style('margin-right:20px ');
QSB.style('border-radius:5px ');
QSB.style('box-shadow: 0 0 40px 40px #00ced1 inset, 0 0 0 0 #00ffff');
BSB.style('border-color:#00ffff');
BSB.style('color: black');
BSB.style('margin-right:20px ');
BSB.style('border-radius:5px ');
BSB.style('box-shadow: 0 0 40px 40px #00ced1 inset, 0 0 0 0 #00ffff');
ISB.style('border-color:#00ffff');
ISB.style('color: black');
ISB.style('margin:10px 20px 0px 0px ');
ISB.style('border-radius:5px ');
ISB.style('box-shadow: 0 0 40px 40px #00ced1 inset, 0 0 0 0 #00ffff');
SSB.style('border-color:#00ffff');
SSB.style('color: black');
SSB.style('margin-right:20px ');
SSB.style('border-radius:5px ');
SSB.style('box-shadow: 0 0 40px 40px #00ced1 inset, 0 0 0 0 #00ffff');
arr = new Array(100);
for (let i = 0; i < arr.length; i++) {
arr[i] = random(5, 500);
}
//calling back respective functions on button click event
QSB.mousePressed(QuickSort);
BSB.mousePressed(BubbleSort);
ISB.mousePressed(InsertionSort);
SSB.mousePressed(SelectionSort);
}
function draw() {
background(100, 150, 200);
stroke(255);
for (let i = 0; i < arr.length; i++) {
if (bool[i] === 1) {
fill(r,g,b);
} else if (bool[i] == 2) {
fill("red");
} else {
fill(20);
}
rect(i * w, height - arr[i], w, arr[i]);
}
}
//QUICKSORT ALGORITHM
async function quickSort(arr, start, end) {
if (start >= end) {
return;
}
let pivot_index = await findpivot(arr, start, end);
bool[pivot_index] = 0;
await Promise.all([quickSort(arr, start, pivot_index - 1),
quickSort(arr, pivot_index + 1, end)
]);
}
async function findpivot(arr, start, end) {
for (let i = start; i < end; i++) {
bool[i] = 2;
}
var pivot = start;
bool[pivot] = 1; //to mark a index as pivot.
var ele = arr[end];
for (let i = start; i < end; i++) {
if (arr[i] < ele) {
await swap(arr, i, pivot);
bool[pivot] = 0; //setting back to 0 as pivot is about to advance
pivot++;
}
}
for (let i = start; i < end; i++) {
bool[i] = 0;
}
await swap(arr, end, pivot);
return pivot;
}
async function QuickSort() {
if (running == -1) {
running = 1;
for (let i = 0; i < arr.length; i++) {
arr[i] = random(5, 500);
bool[i] = 0;
}
r=255;
g=255;
b=255;
await quickSort(arr, 0, arr.length - 1);
running = -1;
}
}
//Bubble sort
async function bubbleSort(arr) {
for (let i = arr.length - 1; i >= 0; i--) {
for (let j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
bool[j] = 1;
await swap(arr, j, j + 1);
bool[j] = 0;
}
bool[i+1] = 1;
}
}
}
async function BubbleSort() {
if (running == -1) {
running = 1;
for (let i = 0; i < arr.length; i++) {
arr[i] = random(5, 500);
bool[i] = 0;
}
r=0;
b=155;
g=250;
await bubbleSort(arr);
running = -1;
}
}
//Insertion sort
async function insertionSort(arr) {
for (let i=1; i<arr.length; ++i)
{
let key = arr[i];
let j = i-1;
//Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position
while (j>=0 && arr[j] > key)
{
arr[j+1] = arr[j];
bool[j]=1;
j = j-1;
}
await sleep(100);
arr[j+1] = key;
}
}
async function InsertionSort() {
if (running == -1) {
running = 1;
for (let i = 0; i < arr.length; i++) {
arr[i] = random(5, 500);
bool[i] = 0;
}
r=70;
g=210;
b=205;
await insertionSort(arr);
running = -1;
}
}
//selection sort
async function selectionSort(arr){
for (let i = 0; i < arr.length-1; i++)
{
// Find the minimum element in unsorted array
let min_idx = i;
for (let j = i+1; j < arr.length; j++){
if (arr[j] < arr[min_idx]) {
min_idx = j;
bool[min_idx]=1;
}
}
bool[min_idx]=0;
bool[i]=1;
await swap(arr,min_idx,i);
}
}
async function SelectionSort() {
if (running == -1) {
running = 1;
for (let i = 0; i < arr.length; i++) {
arr[i] = random(5, 500);
bool[i] = 0;
}
r=150;
g=0;
b=0;
await selectionSort(arr, 0, arr.length - 1);
running = -1;
}
}
//SWAP FUNCTION
async function swap(arr, i, j) {
await sleep(50);
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
//FUNCTION TO AWAIT FOR A SPECIFIC TIME
//usefull to set some time delay for a process.so, we can see theworking of algorithms in real time.
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}