-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
269 lines (258 loc) · 10.3 KB
/
Copy pathscript.js
File metadata and controls
269 lines (258 loc) · 10.3 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
function generateRandomArray(size) {
return Array.from({ length: size }, () => Math.floor(Math.random() * 99) + 1);
}
function drawBars(array) {
const arrayContainer = document.getElementById('array-container');
arrayContainer.innerHTML = '';
array.forEach(height => {
const bar = document.createElement('div');
bar.className = 'bar';
bar.style.height = `${height}px`;
const label = document.createElement('span');
label.className = 'bar-label';
label.textContent = height;
bar.appendChild(label);
arrayContainer.appendChild(bar);
});
}
const array = generateRandomArray(30);
drawBars(array);
//Randomize
const random = document.querySelector('#randomize');
random.addEventListener('click', () => {
const array = generateRandomArray(30);
drawBars(array);
});
// BubbleSort
async function bubbleSort(speed) {
const arrayContainer = document.getElementById('array-container');
const bars = arrayContainer.children;
let array = Array.from(bars).map(bar => parseInt(bar.style.height));
const delayFactor = 100 / speed;
for (let i = 0; i < array.length - 1; i++) {
for (let j = 0; j < array.length - i - 1; j++) {
bars[j].style.backgroundColor = '#e74c3c';
bars[j+1].style.backgroundColor = '#e74c3c';
await new Promise(resolve => setTimeout(resolve, delayFactor));
if (array[j] > array[j+1]) {
[array[j], array[j+1]] = [array[j+1], array[j]];
bars[j].style.height = `${array[j]}px`;
bars[j].querySelector('.bar-label').textContent = array[j];
bars[j+1].style.height = `${array[j+1]}px`;
bars[j+1].querySelector('.bar-label').textContent = array[j+1];
}
bars[j].style.backgroundColor = '#3498db';
bars[j+1].style.backgroundColor = '#3498db';
}
}
}
document.getElementById('bubble').addEventListener('click', () => {
const speed = document.getElementById('speed').value;
bubbleSort(speed);
});
document.getElementById('speed').addEventListener('input', function () {
const newSpeed = this.value;
});
//SelectionSort
async function SelectionSort(speed){
const arrayContainer = document.getElementById('array-container');
const bars = arrayContainer.children;
let array = Array.from(bars).map(bar => parseInt(bar.style.height));
const delayFactor = 100 / speed;
for(let i=0;i<array.length;i++){
let min_ind=i
for(let j=i+1;j<array.length;j++){
bars[j].style.backgroundColor = '#e74c3c';
await new Promise(resolve => setTimeout(resolve,delayFactor));
if(array[min_ind]>array[j]){
[array[min_ind],array[j]]=[array[j],array[min_ind]]
bars[j].style.height = `${array[j]}px`;
bars[j].querySelector('.bar-label').textContent = array[j];
bars[min_ind].style.height = `${array[min_ind]}px`;
bars[min_ind].querySelector('.bar-label').textContent = array[min_ind];
}
bars[j].style.backgroundColor = '#3498db';
}
}
}
document.getElementById('selection').addEventListener('click',()=>{
const speed=document.getElementById('speed').value;
SelectionSort(speed)
})
//InsertionSort
async function InsertionSort(speed) {
const arrayContainer = document.getElementById('array-container');
const bars = arrayContainer.children;
let array = Array.from(bars).map(bar => parseInt(bar.style.height));
const delayFactor = 100 / speed;
for (let i = 1; i < array.length; i++) {
let key = array[i];
let j = i - 1;
while (j >= 0 && key<array[j]) {
bars[j].style.backgroundColor='#e74c3c';
bars[j+1].style.backgroundColor='#e74c3c';
await new Promise(resolve=>setTimeout(resolve, delayFactor));
array[j+1]=array[j];
bars[j+1].style.height=`${array[j+1]}px`;
bars[j+1].querySelector('.bar-label').textContent=array[j+1];
bars[j].style.backgroundColor='#3498db';
bars[j+1].style.backgroundColor='#3498db';
j=j-1;
}
array[j+1]=key;
bars[j+1].style.height=`${key}px`;
bars[j+1].querySelector('.bar-label').textContent=key;
bars[j+1].style.backgroundColor='#3498db';
}
}
document.getElementById('insertion').addEventListener('click',()=>{
const speed=document.getElementById('speed').value;
InsertionSort(speed);
});
//quicksort
async function partition(arr,low,high,speed,bars) {
const pivot=arr[high];
let i=low-1;
const delayFactor=100/speed;
for (let j=low;j<=high-1;j++){
bars[j].style.backgroundColor='#e74c3c';
bars[high].style.backgroundColor='#e74c3c';
await new Promise(resolve=>setTimeout(resolve,delayFactor));
if(arr[j]<pivot) {
i++;
[arr[i],arr[j]]=[arr[j],arr[i]];
[bars[i].style.height, bars[j].style.height]=[bars[j].style.height, bars[i].style.height];
const labelI=bars[i].querySelector('.bar-label').textContent;
const labelJ=bars[j].querySelector('.bar-label').textContent;
bars[i].querySelector('.bar-label').textContent=labelJ;
bars[j].querySelector('.bar-label').textContent=labelI;
}
bars[j].style.backgroundColor='#3498db';
bars[high].style.backgroundColor='#3498db';
}
[arr[i+1],arr[high]]=[arr[high],arr[i+1]];
[bars[i+1].style.height,bars[high].style.height]=[bars[high].style.height,bars[i+1].style.height];
const labelIPlus1=bars[i+1].querySelector('.bar-label').textContent;
const labelHigh=bars[high].querySelector('.bar-label').textContent;
bars[i+1].querySelector('.bar-label').textContent=labelHigh;
bars[high].querySelector('.bar-label').textContent=labelIPlus1;
return i+1;
}
async function quickSort(arr,low,high,speed,bars){
if (low<high){
const pi=await partition(arr, low, high, speed, bars);
await Promise.all([
quickSort(arr,low,pi-1,speed,bars),
quickSort(arr,pi+1,high,speed,bars)
]);
}
}
document.getElementById('quick').addEventListener('click',async()=>{
const speed=document.getElementById('speed').value;
const containBars=document.getElementById('array-container').children;
const array=Array.from(containBars).map(bar=>parseInt(bar.style.height));
await quickSort(array,0,array.length-1,speed,containBars);
});
//MergeSort
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function mergeSort(arr, bars, speedFactor) {
const n = arr.length;
if (n < 2) {
return arr;
}
const left = arr.slice(0, Math.floor(n / 2));
const right = arr.slice(Math.floor(n / 2));
await mergeSort(left, bars, speedFactor);
await mergeSort(right, bars, speedFactor);
let i = 0,
j = 0,
k = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
arr[k] = left[i];
bars[k].style.height = `${arr[k]}px`;
bars[k].style.backgroundColor = '#e74c3c';
bars[k].querySelector('.bar-label').textContent = arr[k];
await sleep(speedFactor);
i++;
} else {
arr[k] = right[j];
bars[k].style.height = `${arr[k]}px`;
bars[k].style.backgroundColor = '#e74c3c';
bars[k].querySelector('.bar-label').textContent = arr[k];
await sleep(speedFactor);
j++;
}
bars[k].style.backgroundColor = '#3498db';
k++;
}
while (i < left.length) {
arr[k] = left[i];
bars[k].style.height = `${arr[k]}px`;
bars[k].style.backgroundColor = '#e74c3c';
bars[k].querySelector('.bar-label').textContent = arr[k];
await sleep(speedFactor);
i++;
k++;
}
while (j < right.length) {
arr[k] = right[j];
bars[k].style.height = `${arr[k]}px`;
bars[k].style.backgroundColor = '#e74c3c';
bars[k].querySelector('.bar-label').textContent = arr[k];
await sleep(speedFactor);
j++;
k++;
}
for (let i = 0; i < bars.length; i++) {
bars[i].style.backgroundColor = '#3498db';
}
return arr;
}
document.getElementById('merge').addEventListener('click', async () => {
const speed = document.getElementById('speed').value;
const containBars = document.getElementById('array-container').children;
const array = Array.from(containBars).map(bar => parseInt(bar.style.height));
const speedFactor = 100 / speed;
await mergeSort(array, containBars, speedFactor);
});
//shellSort
async function shellSort() {
const arrayContainer=document.getElementById('array-container');
const bars=arrayContainer.children;
const speed=document.getElementById('speed').value;
const delayFactor=100/speed
const n=bars.length;
let gap=1;
while(gap<n/3) {
gap=3*gap+1;
}
while(gap>0) {
for(let i=gap;i<n;i++) {
const currentHeight=parseInt(bars[i].style.height);
let j=i;
while(j>=gap && parseInt(bars[j-gap].style.height) > currentHeight) {
bars[j].style.backgroundColor='#e74c3c';
bars[j-gap].style.backgroundColor='#e74c3c';
await new Promise(resolve=>setTimeout(resolve,delayFactor));
bars[j].style.height=bars[j-gap].style.height;
bars[j-gap].style.height=`${currentHeight}px`;
const tempLabel=bars[j].querySelector('.bar-label').textContent;
bars[j].querySelector('.bar-label').textContent=bars[j-gap].querySelector('.bar-label').textContent;
bars[j-gap].querySelector('.bar-label').textContent=tempLabel;
bars[j].style.backgroundColor = '#3498db';
bars[j-gap].style.backgroundColor = '#3498db';
j-=gap;
}
}
gap=Math.floor(gap/3);
}
bars.forEach((bar,index) => {
setTimeout(() => {
bar.style.backgroundColor='#2ecc71';
}, index*(100/delayFactor));
});
}
document.getElementById('shell').addEventListener('click', shellSort);