-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
227 lines (221 loc) · 6.56 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
//constants/globals
let arr = [];
const width = window.innerWidth;
const height = window.innerHeight - 75;
let thick = 20;
let len_arr = Math.floor(width / thick);
let sorted_arr = [];
let start_sorting = false;
let frame_rate_val = 40;
let timer_arr = [];
let time_taken = 0;
let pause = false;
//each bar has following properties
class Element {
constructor(val) {
this.val = val;
this.compare = false;
this.swap = false;
this.sub_arr = false;
}
//compare(blue), swap(red), nop(green)
design(i, color = 255) { // black
fill(color);
if (this.compare == true) {
fill(0, 0, 255);
}
if (this.swap == true) {
fill(255, 0, 0);
}
stroke(0);
this.swap = false;
this.compare = false; //reset the vals as i only want one time display
this.pivot = false; //waste delete it later(arr.pivot prop)
rect(i * thick, height - this.val + 1, thick, this.val);
// ellipse(i * thick + 20,height - this.val + 10,10,this.val - height);
console.log(this.val);
if (thick > 5) {
noStroke(); //mix with rect
ellipse(
i * thick + thick / 2 + 0.5,
height - this.val + 1,
thick - 1
);
//not draw when rect thin
}
}
}
//function name holder
const algo_dict = {
bubbleSort: bubbleSort,
selectionSort: selectionSort,
mergeSort: mergeSort,
quickSort: quickSortLomuto,
};
const timer_algo = {
bubbleSort: bubbleSort_t,
selectionSort: selectionSort_t,
mergeSort: mergeSort_t,
quickSort: quickSortLomuto_t,
};
function setup() {
print_hello();
createCanvas(width, height);
var btns = document.querySelectorAll(".clickable"); //all buttons
// console.log(btns);
for (btn of btns) {
btn.addEventListener("click", function () {
// console.log("clicked", this.id); debugging button
if (this.id == "reset") {
//gets a new arr
//styles the element to default
arr = [];
sorted_arr = [];
start_sorting = false;
frameRate(frame_rate_val);
setup_arr();
document.getElementById("time").innerHTML = "Time: 0us";
document.getElementById("frm").value = "40";
} else {
if (this.id != "") {
if (start_sorting == false) {
//if no other sorting algo was selected
start_sorting = true;
start_sort(this.id);
time_this_algo(this.id);
frameRate(frame_rate_val);
} else {
//if other algo was running
//reset and run this new algo
arr = [];
sorted_arr = [];
start_sorting = false;
frameRate(frame_rate_val);
setup_arr();
document.getElementById("time").innerHTML = "Time:0us";
start_sorting = true;
time_this_algo(this.id);
start_sort(this.id);
}
}
}
return true;
});
}
slider_control(); //slide control
setup_arr(); //create the arr
// console.log(frameRate());
}
function time_this_algo(algo) {
//timer start
var t0 = window.performance.now();
timer_loop = timer_algo[algo](timer_arr);
var t1 = window.performance.now();
console.log(t1 - t0);
time = (t1 - t0) * 1000;
time = Math.round(time);
t1 = 0;
t0 = 0;
if (time == 0) {
time = 0.01;
}
if (time < 1000) {
document.getElementById("time").innerHTML =
"Time: " + String(time) + "us";
} else {
document.getElementById("time").innerHTML =
"Time: " + String(Math.round(time / 1000)) + "ms";
}
//timer stop
//store the diff
}
function slider_control() {
var size_slider = document.getElementById("data_size");
size_slider.oninput = function () {
thick = 62 - size_slider.value;
//higher the slider more the bars
len_arr = Math.floor(width / thick);
//reset everything
arr = [];
sorted_arr = [];
start_sorting = false;
frameRate(frame_rate_val);
setup_arr();
// console.log(arr);
};
var frm_slider = document.getElementById("frm");
// console.log(frm_slider);
frm_slider.oninput = function () {
//cant cange vals superfast
//so we do according to range of slider
if (this.value == 0) {
frame_rate_val = 0;
frameRate(0);
}
if (1 < this.value && this.value <= 10) {
if (frameRate() != 5) {
frame_rate_val = 5;
frameRate(5);
}
}
if (11 < this.value && this.value <= 20) {
if (frameRate() != 20) {
frame_rate_val = 20;
frameRate(20);
}
}
if (21 < this.value && this.value <= 30) {
if (frameRate() != 40) {
frame_rate_val = 40;
frameRate(40);
}
}
if (35 < this.value && this.value <= 40) {
if (frameRate() != 60) {
frame_rate_val = 60;
frameRate(60);
}
}
};
}
function setup_arr() {
for (let i = 0; i < len_arr; i++) {
push_value = random(thick, height - thick); //ellipse height
arr.push(new Element(push_value));
sorted_arr.push(push_value);
timer_arr.push(new Element(push_value));
}
sort_the_arr(sorted_arr);
}
function start_sort(algo) {
loop_counter = algo_dict[algo](arr);
}
function draw_arr() {
for (let i = 0; i < arr.length; i++) {
arr[i].design(i);
if (sorted_arr[i] == arr[i].val) {
arr[i].design(i, color(0, 255, 0));
}
}
}
function draw() {
background(0);
if (start_sorting == true) {
loop_counter.next();
}
draw_arr();
}
function sort_the_arr(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
}
function print_hello() {
console.log('Hellooo human!');
}