forked from aryaraj132/SortingVisualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
248 lines (246 loc) · 7.99 KB
/
app.js
File metadata and controls
248 lines (246 loc) · 7.99 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
Bar_size = Math.floor((window.innerWidth - 120)/15);;
timeout = 100
document.getElementById('fast').addEventListener('click',()=>{
timeout=30
});
document.getElementById('default-speed').addEventListener('click',()=>{
timeout=100
});
document.getElementById('slow').addEventListener('click',()=>{
timeout=500
});
function randomInt(min, max) {
return Math.floor(Math.random() * (max-min +1) + min);
}
Arr = [];
function timer(ms) { return new Promise(res => setTimeout(res, ms)); }
function GenArray() {
let bars = document.querySelectorAll('.bars');
for (let index = 0; index < Bar_size; index++) {
const element = bars[index];
Arr[index] = randomInt(5,90);
element.style.height = Arr[index] + "%";
element.style.background = "aqua";
}
}
function draw(array){
bars = document.querySelectorAll('.bars');
for (let i = 0; i < array.length; i++) {
const element = array[i];
bars[i].style.background = "Violet";
bars[i].style.height = element + "%";
}
}
async function mergeSort(array, leftIndex, rightIndex) {
length = rightIndex - leftIndex
if (length < 2) {
return array;
}
var mid = leftIndex + Math.floor(length / 2);
mergeSort(array, leftIndex, mid)
mergeSort(array, mid, rightIndex)
await timer(timeout)
draw(array);
merge(array, leftIndex, mid, rightIndex)
}
async function merge(array, leftIndex, mid, rightIndex) {
bars = document.querySelectorAll('.bars');
var result = [];
var l = leftIndex,
r = mid,
barIndex = 0;
while (l < mid && r < rightIndex) {
if (array[l] < array[r]) {
result.push(array[l++]);
//await timer(timeout);
//bars[barIndex].style.height = result[barIndex] + "%";
//barIndex++;
} else {
result.push(array[r++]);
//await timer(timeout);
//bars[barIndex].style.height = result[barIndex] + "%";
//barIndex++;
}
}
while (l<mid) {
result.push(array[l++]);
//bars[barIndex].style.height = result[barIndex] + "%";
//barIndex++;
}
while (r<rightIndex) {
result.push(array[r++]);
//bars[barIndex].style.height = result[barIndex] + "%";
//barIndex++;
}
for (let i = 0; i < rightIndex - leftIndex; i++) {
array[leftIndex + i] = result[i]
}
//for (let i = 0; i < result.length; i++) {
// const element = result[i];
// await timer(timeout)
// bars[i].style.height = element + "%";
//}
}
async function partition(items, left, right,bars) {
let mid = Math.floor((right + left) / 2);
var pivot = items[mid],
i = left,
j = right;
bars[mid].style.background = "green";
while (i <= j) {
while (items[i] < pivot) {
i++;
}
while (items[j] > pivot) {
j--;
}
if (i <= j) {
await timer(timeout*(2/3));
bars[j].style.background = "red";
bars[i].style.background = "red";
let temp = items[i]
items[i] = items[j]
items[j] = temp
await timer(timeout*(2/3));
bars[i].style.height = items[i] + "%";
bars[j].style.height = items[j] + "%";
await timer(timeout*(2/3));
bars[j].style.background = "aqua";
bars[i].style.background = "aqua";
i++;
j--;
}
}
bars[mid].style.background = "aqua";
return i;
}
async function quickSort(items, left, right) {
bars = document.querySelectorAll('.bars');
var index;
if (items.length > 1) {
index = await partition(items, left, right,bars); //index returned from partition
if (left < index - 1) { //more elements on the left side of the pivot
quickSort(items, left, index - 1);
}
if (index < right) { //more elements on the right side of the pivot
quickSort(items, index, right);
}
}
return items;
}
async function insertionSort(inputArr) {
bars = document.querySelectorAll('.bars');
let n = inputArr.length;
for (let i = 1; i < n; i++) {
let current = inputArr[i];
let j = i-1;
bars[i].style.background = "green";
await timer(timeout)
while ((j > -1) && (current < inputArr[j])) {
inputArr[j+1] = inputArr[j];
bars[j+1].style.background = "red";
await timer(timeout)
bars[j+1].style.height = inputArr[j+1] + "%";
bars[j+1].style.background = "aqua";
j--;
}
inputArr[j+1] = current;
bars[j+1].style.height = inputArr[j+1] + "%";
bars[j+1].style.background = "green";
bars[i].style.background = "aqua";
}
for(let i=n-1;i>=0;i--){
await timer(50)
bars[i].style.background = "violet";
}
return inputArr;
}
async function selectionSort(inputArr) {
let n = inputArr.length;
bars = document.querySelectorAll('.bars');
for(let i = 0; i < n; i++) {
let min = i;
bars[i].style.background = "green";
for(let j = i+1; j < n; j++){
bars[j].style.background = "green";
if(inputArr[j] < inputArr[min]) {
bars[j].style.background = "red";
min=j;
}
await timer(timeout*(2/3))
bars[j].style.background = "aqua";
}
if (min != i) {
await timer(timeout*(2/3))
bars[min].style.background = "red";
bars[i].style.background = "red";
let tmp = inputArr[i];
inputArr[i] = inputArr[min];
bars[i].style.height = inputArr[i] + "%";
inputArr[min] = tmp;
bars[min].style.height = inputArr[min] + "%";
await timer(timeout*(2/3))
bars[min].style.background = "aqua";
bars[i].style.background = "aqua";
}
}
for (let i = 0; i < bars.length; i++) {
const element = bars[i];
await timer(50)
element.style.background = 'violet';
}
return inputArr;
}
async function bubbleSort(inputArr){
let len = Bar_size;
bars = document.querySelectorAll('.bars');
for (let i = 0; i < len; i++) {
for (let j = 0; j < len-1-i; j++) {
bars[j].style.background = "green";
bars[j+1].style.background = "green";
if (inputArr[j] > inputArr[j + 1]) {
let tmp = inputArr[j];
inputArr[j] = inputArr[j + 1];
inputArr[j + 1] = tmp;
await timer(timeout/2);
bars[j].style.background = "red";
bars[j+1].style.background = "red";
bars[j].style.height = Arr[j] + "%";
bars[j+1].style.height = Arr[j+1] + "%";
}
await timer(timeout/2);
bars[j].style.background = "aqua";
bars[j+1].style.background = "aqua";
}
bars[len-1-i].style.background = "violet";
}
return inputArr;
};
document.getElementById('bubbleSort').addEventListener('click', ()=>{
bubbleSort(Arr);
});
document.getElementById('insertionSort').addEventListener('click', ()=>{
insertionSort(Arr);
});
document.getElementById('selectionSort').addEventListener('click', ()=>{
selectionSort(Arr);
});
document.getElementById('quickSort').addEventListener('click', ()=>{
quickSort(Arr,0,Arr.length-1);
});
document.getElementById('mergeSort').addEventListener('click', async()=>{
await mergeSort(Arr,0,Arr.length);
await timer(timeout);
draw(Arr);
console.log(Arr)
});
window.onload = function(){
if (window.innerWidth < 170) {
Bar_size = 4;
}
ul = document.querySelector('.bars-container');
for (let i = 0; i < Bar_size; i++) {
ul.insertAdjacentHTML( 'afterbegin', '<li class="bars"></li>');
}
GenArray();
};