-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
322 lines (282 loc) · 8.39 KB
/
Copy pathscript.js
File metadata and controls
322 lines (282 loc) · 8.39 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
var arr = [];
var table = document.getElementById('table');
const css_root = document.querySelector(':root')
css_root.style.setProperty('--back', 'linear-gradient(#1e1e2e, #1e1e3e)');
css_root.style.setProperty('--fore', '#7babfa');
function switchTheme() {
const theme = document.getElementById('theme').value
const css_root = document.querySelector(':root')
switch (theme) {
case 'mc':
css_root.style.setProperty('--back', 'black')
css_root.style.setProperty('--fore', 'white')
break
case 'og':
css_root.style.setProperty('--back', '#1e1e2e')
css_root.style.setProperty('--fore', '#89b4fa')
break
case 'sz':
css_root.style.setProperty('--back', 'linear-gradient(#151515, #303030)')
css_root.style.setProperty('--fore', '#bfbfbf')
break
case 'mt':
css_root.style.setProperty('--back', 'linear-gradient(#242c2c, #2a3333)')
css_root.style.setProperty('--fore', '#67bcc2')
break
case 'tm':
css_root.style.setProperty('--back', 'linear-gradient(#67bcc2, #53999d)')
css_root.style.setProperty('--fore', '#2a3333')
break
case 'pw':
css_root.style.setProperty('--fore', 'linear-gradient(#d2ca56, #bcb54d)')
css_root.style.setProperty('--back', '#b45f81')
break
}
}
function generate() {
// * Clearing window for next generation
for (let i=3; i<1000; i++) {
window.clearInterval(i);
}
// * Getting amount of bars to create and where to create bars
var size = document.getElementById('size').value;
// * Clearing the array to make this function usable more than once
arr = [];
// * If there is already bars on screen making them dissapear
[].slice.call(table.children).forEach(bar => {
bar.style.maxHeight = '0';
});
var ti = 500;
if(table.innerHTML=='') ti = 0
table.innerHTML = ''
// * Creating bars
for (var i = 0; i < size; i++) {
var bar = document.createElement('div');
bar.classList.add('bar');
bar.id = i
table.appendChild(bar);
}
// * Adding them to our array to sort and make them appear on the screen
setTimeout(() => {
[].slice.call(table.children).forEach(bar => {
bar_height = `${parseFloat((Math.random() * 100))}%`;
bar.style.maxHeight = bar_height;
var value = [parseInt(bar.id), bar_height.replace('%', '')];
arr.push(value)
});
}, 100);
}
function timer(array) {
for(i=0; i<array.length; i++) {
const bar = document.getElementById(i);
//bar.style.transition = '0s';
bar.style.transition = 'max-height .1s';
bar.style.maxHeight = array[i].toString()+'%';
}
}
// * Calculating which algorithm to use
function sort() {
var heights = []
const dropdown = document.getElementById('dropdown').value;
// * Creating array to sort with name heights
arr.forEach(subarr => {
heights.push(parseInt(subarr[1]))
});
//heights.sort(function(a, b){return a - b});
const delay = document.getElementById('delay').value
switch (dropdown) {
case 'marge':
mergeSort(heights, 0,heights.length-1);
timer(heights);
break;
case 'bubble':
bubbleSort(heights, delay);
break;
case 'insertion':
insertionSort(heights, delay);
break;
case 'selection':
selectionSort(heights, delay);
break;
case 'heap':
heapSort(heights, delay);
break;
case 'merge':
let cases = mergeSort(heights, delay);
a = 1
cases.forEach(cas => {
setTimeout(() => {
timer(cas)
}, delay*a)
a++
});
break;
case 'quick':
quickSort(heights, 0, heights.length-1, delay)
break;
case 'radix':
radixSort(heights)
break;
default:
break;
}
}
function mergeSort(array) {
var arrays = [array.slice()],
n = array.length,
array0 = array,
array1 = new Array(n);
for (var m = 1; m < n; m <<= 1) {
for (var i = 0; i < n; i += (m << 1)) {
merge(i, Math.min(i + m, n), Math.min(i + (m << 1), n));
}
arrays.push(array1.slice());
array = array0, array0 = array1, array1 = array;
}
function merge(left, right, end) {
for (var i0 = left, i1 = right, j = left; j < end; ++j) {
array1[j] = array0[i0 < right && (i1 >= end || array0[i0] <= array0[i1]) ? i0++ : i1++];
}
}
return arrays
}
function bubbleSort(arr, delay) {
console.clear()
let len = arr.length;
let checked = false;
do {
checked = false;
setInterval(() => {
for (let i = 0; i < len; i++) {
if (arr[i] > arr[i + 1]) {
let tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
checked = true;
}
if (i == 0) {
timer(arr)
}
}
}, delay);
} while (checked);
}
function insertionSort(arr, delay) {
console.clear()
var cases = [];
let len = arr.length;
for (let i = 1; i < len; i++) {
let j = i - 1
let temp = arr[i]
while (j >= 0 && arr[j] > temp) {
arr[j + 1] = arr[j]
j--
}
arr[j+1] = temp
cases.push(arr.toString())
}
a = 1
cases.forEach(cas => {
setTimeout(() => {
cas = cas.split(',')
cas.forEach(val => parseInt(val))
timer(cas)
}, delay*a)
a++
});
}
function selectionSort(arr, delay) {
let len = arr.length;
a = 1
for(let i = 0; i < len; i++) {
setTimeout(() => {
let min = i;
for(let j = i+1; j < len; j++){
if(arr[j] < arr[min]) {
min=j;
}
}
if (min != i) {
let tmp = arr[i];
arr[i] = arr[min];
arr[min] = tmp;
}
timer(arr)
}, delay*a)
a++
}
}
var array_length;
function heap_root(input, i) {
var left = 2 * i + 1;
var right = 2 * i + 2;
var max = i;
if (left < array_length && input[left] > input[max]) {
max = left;
}
if (right < array_length && input[right] > input[max]) {
max = right;
}
if (max != i) {
swap(input, i, max);
heap_root(input, max);
}
}
function swap(input, index_A, index_B) {
var temp = input[index_A];
input[index_A] = input[index_B];
input[index_B] = temp;
}
function heapSort(arr, delay) {
console.clear();
array_length = arr.length;
let cases = [];
let a = 0;
for (var i = Math.floor(array_length / 2); i >= 0; i -= 1) {
heap_root(arr, i);
}
for (i = arr.length - 1; i > 0; i--) {
swap(arr, 0, i);
array_length--;
heap_root(arr, 0);
cases.push(arr.toString());
}
cases.forEach(cas => {
setTimeout(() => {
cas = cas.split(',')
cas.forEach(val => parseInt(val))
timer(cas)
}, delay*a)
a++
});
}
function partition(arr, start, end) {
const pivotValue = arr[start]
let swapIndex = start
for (let i = start + 1; i <= end; i++) {
if (pivotValue > arr[i]) {
swapIndex++
if (i !== swapIndex) {
// SWAP
;[arr[i], arr[swapIndex]] = [arr[swapIndex], arr[i]]
}
}
}
if (swapIndex !== start) {
// Swap pivot into correct place
;[arr[swapIndex], arr[start]] = [arr[start], arr[swapIndex]]
}
timer(arr)
return swapIndex
}
function quickSort(arr, start, end, delay) {
// Base case
if (start >= end) return
setTimeout(() => {
let pivotIndex = partition(arr, start, end)
// Left
quickSort(arr, start, pivotIndex - 1, delay)
// Right
quickSort(arr, pivotIndex + 1, end, delay)
}, delay)
}