-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdown.html
More file actions
238 lines (218 loc) · 6.56 KB
/
down.html
File metadata and controls
238 lines (218 loc) · 6.56 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
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<title>Algorithm Visualization</title>
</head>
<style>
.bars
{
display:flex;
color:white;
justify-content:center;
align-items:flex-end;
padding:5px;
}
.bar
{
margin : 0px 4px;
width:50px;
background:#2979ff;
display:flex;
position:relative;
align-items:center;
justify-content:center;
font-weight:bold;
}
@keyframes left{
from {left: 0px;}
to {left: 50px;
opacity:0;}
}
@keyframes right{
from {right: 50px;}
to {right: 70px;
opacity:1;}
}
</style>
<body>
<center> <h1> Visualize Algorithm </h1> </center>
<div style="display:flex;
justify-content:flex-end;
align-items:center;
flex-direction:column;
height:550px">
<div class="bars" id="bars">
</div>
<div style="width:99%; background:#FF8F00; height:5px; margin:0px; padding:0px; "></div>
<div style="display:flex; ">
</div>
<center>
<input type="text" id="array" placeholder="Enter Array comma seprated" style="padding:10px 10px;margin-top:10px; "/>
<button id="btn" style="padding:13px; color:white; border:0px; background:#2979ff" onclick="CreateArray()">MAKE</button><br><br>
<button id="btn2" style="padding:13px; color:white; border:0px; background:#2979ff" onclick="RandomArray()">Random Array</button>
<button id="btn3" style="padding:13px; color:white; border:0px; background:#2979ff" onclick="javascript:window.open('./index.html','_self');">UpSideDown</button>
<button id="sort" style="display:none; height:46px; color:white; border:0px;margin-top:10px; background:#2979ff " onclick="SelectionSort()" >Selection Sort</button>
<button id="bsort" style="display:none; height:46px; color:white; border:0px; background:#2979ff " onclick="BubbleSort()" >Bubble Sort</button>
<button id="Obsort" style="display:none; height:46px; color:white; border:0px; background:#2979ff " onclick="OBubbleSort()" >Optimized Bubble Sort</button>
</center>
<script>
var arrayString=document.getElementById("array");
var btn=document.getElementById("btn");
var btn3=document.getElementById("btn3");
var btn2=document.getElementById("btn2");
var bars=document.getElementById("bars");
var array=[];
function RandomArray()
{
for(var i=0; i<10; i++)
{
//console.log(parseInt(Math.random()*100));
array[i]=parseInt(Math.random()*100);
}
document.getElementById("sort").style.display="inline-block";
document.getElementById("bsort").style.display="inline-block";
document.getElementById("Obsort").style.display="inline-block";
makeArray();
}
function CreateArray()
{
console.log(arrayString.value.split(","));
array=arrayString.value.split(",");
makeArray();
document.getElementById("sort").style.display="inline-block";
document.getElementById("bsort").style.display="inline-block";
document.getElementById("Obsort").style.display="inline-block";
}
function makeArray()
{
bars.innerHTML="";
for(var i=0; i<array.length; i++)
{
//console.log(array[i]);
var bar = document.createElement("div");
bar.style.height=""+Math.abs(array[i])*3+"px";
bar.className="bar";
bar.id="bar"+(i+1);
var textnode = document.createTextNode(""+array[i]);
if(Math.abs(array[i])<5)
{
bar.style.color="black";
}
bar.appendChild(textnode);
bars.appendChild(bar);
}
btn.style.display="none";
btn3.style.display="none";
btn2.style.display="none";
arrayString.style.display="none";
}
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
async function SelectionSort()
{
for(var i=0; i<array.length; i++)
{
var min=i;
var bar_min=document.getElementById("bar"+(i+1));
bar_min.style.background="#f44336";
for(var j=i+1; j<array.length; j++)
{
document.getElementById("bar"+(j+1)).style.background="#64dd17";
console.log(array[j]+" "+array[min]);
console.log(array[j] < array[min]);
if(parseInt(array[j],10) < parseInt(array[min],10) )
{
document.getElementById("bar"+(i+1)).style.background="#64dd17";
document.getElementById("bar"+(min+1)).style.background="#64dd17";
document.getElementById("bar"+(j+1)).style.background="#f44336";
min=j;
}
console.log(min);
await sleep(1000);
}
var temp=array[min];
array[min]=array[i];
array[i]=temp;
bar_min.style.animation="right 0.5s ease-out";
document.getElementById("bar"+(min+1)).style.animation="left 0.5s ease-out";
/*for(var m=0; m<array.length; m++)
{
document.getElementById("bar"+(m+1)).style.background="#2979ff";
}*/
makeArray();
//swapElements(document.getElementById("bar"+(i+1)),document.getElementById("bar"+(min+1)))
}
}
async function BubbleSort()
{
for(var i=0; i<array.length; i++)
{
for(var j=0; j<array.length-i-1; j++)
{
document.getElementById("bar"+(j+1)).style.background="#64dd17";
document.getElementById("bar"+(j+2)).style.background="#64dd17";
await sleep(500);
if(parseInt(array[j],10)>parseInt(array[j+1],10))
{
document.getElementById("bar"+(j+1)).style.background="#f44336";
document.getElementById("bar"+(j+1)).style.animation="left 0.5s ease-out";
document.getElementById("bar"+(j+2)).style.background="#f44336";
document.getElementById("bar"+(j+2)).style.animation="right 0.5s ease-out";
var temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
await sleep(400);
}
makeArray();
}
makeArray();
}
}
async function OBubbleSort()
{
for(var i=0; i<array.length; i++)
{
var swap = false;
for(var j=0; j<array.length-i-1; j++)
{
document.getElementById("bar"+(j+1)).style.background="#64dd17";
document.getElementById("bar"+(j+2)).style.background="#64dd17";
await sleep(500);
if(parseInt(array[j],10)>parseInt(array[j+1],10))
{
swap = true;
document.getElementById("bar"+(j+1)).style.background="#f44336";
document.getElementById("bar"+(j+1)).style.animation="left 0.5s ease-out";
document.getElementById("bar"+(j+2)).style.background="#f44336";
document.getElementById("bar"+(j+2)).style.animation="right 0.5s ease-out";
var temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
await sleep(400);
}
makeArray();
}
if(!swap)
{
break;
}
makeArray();
}
}
function swapElements(obj1, obj2) {
// create marker element and insert it where obj1 is
var temp = document.createElement("div");
obj1.parentNode.insertBefore(temp, obj1);
// move obj1 to right before obj2
obj2.parentNode.insertBefore(obj1, obj2);
// move obj2 to right before where obj1 used to be
temp.parentNode.insertBefore(obj2, temp);
// remove temporary marker node
temp.parentNode.removeChild(temp);
}
</script>
</div>
</body>
</html>