-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrowtest.html
308 lines (224 loc) · 7.19 KB
/
arrowtest.html
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
<!DOCTYPE html>
<html>
<head>
<title>Animation of Sorting Algorithms</title>
<script type = "text/javascript">
var TargetTick;
var Tick;
var Timer1;
var A; // The array to be sorted
var arr0 = "<span id='arr0' >curr↓</span>";
var arr1 = "<span id='arr1' >min↓</span>";
//var weights=[10,20,30,57,43];
//var values=[60,100,110,43,69];
//A=new Array(weights.length);
A = [79,56,19,44,86,22,41,35,39,99,16,53,29,26,11,49,65,33,51,83];
A = [79,56,19,44,86];
function createfractions()
{
for (var i = 0; i < weights.length; i++)
{
A[i]=(values[i]/weights[i]).toFixed(2);;
//alert(A[i]);
}
}
function ExecuteSort()
{
var Speed = parseInt(speedSelectList.options[speedSelectList.selectedIndex].value);
var Alg = parseInt(algorithmSelectList.options[algorithmSelectList.selectedIndex].value);
TargetTick=1;
//createfractions();
// Start animation
if (Timer1) clearInterval(Timer1);
if (Alg == 0)
Timer1 = setInterval(AnimateSelectionSort,Speed);
else
Timer1 = setInterval(AnimateInsertionSort,Speed);
}
function copyDiv()
{
var firstDivContent = document.getElementById('OutDiv');
var secondDivContent = document.getElementById('SortedElem');
secondDivContent.innerHTML = firstDivContent.innerHTML;
}
function SelectionSort(A)
{
for (var i = 0; i < A.length-1; i++)
{ var min_pos=i ;
var min = A[i];
for(var j = i; j < A.length; j++)
{ if (A[j] < min )
{ min_pos= j; min =A[j] }
if (UpdateTick()) { PrintArray(A,i,j,min_pos); return; }
}
// swap A[i] with A[min_pos]
var t = A[i]; A[i] = A[min_pos]; A[min_pos] = t;
}
// (i > A.length) last call to Printarray() to show array after final swap
PrintArray(A,i+1,-1,-1);
EndAnimate();
}
function InsertionSort(A)
{
for (var i = 1; i < A.length; i++)
{
var item = A[i];
var j = i-1;
// find place for item
// while( (j >= 0) && (item < A[j]))
// for animation, we moved the test for 2nd cond to inside of loop
while ((j >= 0))
{ if (UpdateTick())
{ PrintArray(A,j+1,i,i); return; }
if (item >= A[j]) break;
A[j+1] = A[j];
A[j] = item; // this is added for animation
j--;
}
A[j+1] = item;
}
PrintArray(A,i+1,-1,-1);
EndAnimate();
}
function PrintArray(A, start,target, minpos)
{ // This function renders the array A as an HTML list
// alert(target);
var x = "";
for(var i= 0; i < A.length ; i++)
{ var st = "";
if (i < start) st = " class ='finished'";
var ext ="";
if (i==start)
{ ext = arr0; st = "style='background-color:white'"; }
if (i==target )
{ st = "style='background-color:white'"; }
if (i== minpos)
{ ext += arr1; st = " style='background-color:white;border-color: red'"; }
x += "<li " + st + ">" + A[i] + ext+ "</li>";
}
OutDiv.innerHTML = "<ul>" + x + "</ul>";
}
function PrintGREEDY(A,totweight)
{ // This function renders the array A as an HTML list
// alert(target);
var x = "";
for(var i= 0; i < A.length ; i++)
{
/* var st = "";
if (i < start) st = " class ='finished'";
var ext ="";
if (i==start)
{ ext = arr0; st = "style='background-color:white'"; }
if (i==target )
{ st = "style='background-color:white'"; }
if (i== minpos)
{ ext += arr1; st = " style='background-color:white;border-color: red'"; }
*/
var st="";
var ext ="";
if (A[i]<totweight)
{
st = " class ='placed'";
ext = arr0;
}
else
{
st="class ='finished'";
ext=arr1;
}
x += "<li " + st + ">" + A[i] + ext+ "</li>";
}
var secondDivContent = document.getElementById('SortedElem');
secondDivContent.innerHTML = "<ul>" + x + "</ul>";
}
// The pattern for AnimateSomeProcedure() is:
// Set TragetTick =1 (in caller)
// Execute the procedure to be animated from start (Tick=0) till (Tick=TragetTick)
// Increment TargetTick
// Execute the procedure to be animated from start (Tick=0) till (Tick=TragetTick)
// Increment TargetTick
// Execute the procedure to be animated from start (Tick=0) till (Tick=TragetTick)
// Note: Tick is incremented in UpateTick()
function AnimateSelectionSort()
{ // This function is executed repeatedly via SetInterval()
// Note: TargetTick is incremented with every call to this function
Tick=0;
// SelectionSort(A);
SelectionSort_Rec(A,0) ;
TargetTick++;
}
function AnimateInsertionSort()
{ // This function is executed repeatedly via SetInterval()
// Note: TargetTick is incremented with every call to this function
B = A.slice(); // Start from an original copy of A
Tick=0;
InsertionSort(B);
TargetTick++;
}
function EndAnimate()
{ clearInterval(Timer1);
copyDiv();
PrintGREEDY(A,40);
}
function UpdateTick()
{ Tick++;
return (Tick == TargetTick);
}
function SelectionSort_Rec(A,i)
{
if (i >= A.length-1)
{ // (i > A.length) last call to Printarray() to show array after final swap
PrintArray(A,i+1,-1,-1);
EndAnimate();
return;
}
var min_pos=i ;
var min = A[i];
for(var j = i; j < A.length; j++)
{ if (A[j] < min )
{ min_pos= j; min =A[j]; }
if (UpdateTick()) { PrintArray(A,i,j,min_pos); return; }
}
// swap A[i] with A[min_pos]
var t = A[i]; A[i] = A[min_pos]; A[min_pos] = t;
SelectionSort_Rec(A,i+1); // Recursive call
}
</script>
<style>
body { margin-left:10px; }
ul { list-style-type: none;padding:12px 0px; }
ul li {position:relative; border:1px solid gray; display:inline-block; width:36px; height:36px;
text-align:center; line-height:36px;margin-left:20px; border-radius: 18px;background-color:lightgray}
.target { color:red; background:white}
.finished { background:lightyellow }
.placed { background:blue }
#arr0 , #arr1 { position:absolute; top:-2em; left:0px; padding:0; margin:0; font-weight:bold; }
#arr0 { color: blue;left:-15px;}
#arr1 { color: red; left:-8px;}
label, select, input[type=button] { font-size:1.1em; margin-left:8px; }
input[type=button] { margin-left:16px; padding:1px 12px; }
select { font-size:1.1em; }
label { font-style: italic; }
#OutDiv { margin-top:40px; }
</style>
</head>
<body>
<h1>Animation of Selection Sort and Insertion Sort Algorithms</h1>
<label>Speed <select id="speedSelectList" >
<option value="2000" >slow</option>
<option value="600" >medium</option>
<option value="20" >fast</option>
</select>
</label>
<label>Algorithm <select id="algorithmSelectList" >
<option value="0" >Selection Sort</option>
<option value="1" >Insertion Sort</option>
</select>
</label>
<input type="button" value="Start" onclick="ExecuteSort()" />
<div id="OutDiv" >
</div>
<div id="SortedElem">
</div>
</body>
</html>