forked from kets99/simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection.js
More file actions
170 lines (115 loc) · 4.08 KB
/
Copy pathselection.js
File metadata and controls
170 lines (115 loc) · 4.08 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
// function sleep(milliseconds) {
// var start = new Date().getTime();
// for (var i = 0; i < 1e7; i++) {
// if ((new Date().getTime() - start) > milliseconds){
// break;
// }
// }
// }
async function Selection_Sort(arr, compare_Function) {
function compare(a, b) {
return b - a;
}
var min = 0;
var index = 0;
var temp = 0;
// let promise = new Promise((resolve, reject) => {
// setTimeout(() => resolve("done!"), 4000)
// });
//{Function} compare_Function Compare function
compare_Function = compare_Function || compare;
for (var i = 0; i < arr.length; i += 1) {
fin = Number(arr.length-1);
j_val = Number(i);
await sleep(time);
//sleep(1);
var a = locations[i] ;
var bb = locations[i].elem ;
var bb1 = locations[i].text ;
bb.attr({
fill : "red",
});
await sleep(time);
//sleep(1);
//this , is just like .. going through every position .. and trying to find the correct element for there
//this is achieved by finding the i th max or i th min element dep on whether the sort is asc or desc :)
index = i;
min = arr[i];
document.getElementById('l2').innerHTML = ' for j = '+j_val+' to '+fin;
document.getElementById('l21').innerHTML = ' if (list[j] > minimum ) <br> mimimum = list[j]';
console.log("temp"+arr[i]);
for (var j = i + 1; j < arr.length; j += 1) {
console.log("well hello"+i);
if (compare_Function(min, arr[j]) > 0) {
//for desc , use >
//for asc order , use <
document.getElementById("l21").className = "highl";
min = arr[j];
index = j;
}
}
console.log("tempww"+arr[index]);
var b = locations[index] ;
var t = locations[index].elem ;
var t1 = locations[index].text ;
t.attr({
fill : "green",
});
await sleep(time);
//sleep(1);
bb.animate({ 'width': 50, 'height': 50, 'fill': 'white', 'x': 50*index, 'y': 30 }, 4000, "easeInOut" );
bb1.animate({ 'width': 50, 'height': 50,'x': 50*index + 28, 'y': 58 , 'title' : min }, 4000, "easeInOut" );
t.animate({ 'width': 50, 'height': 50, 'fill': 'white', 'x': 50*i , 'y': 30 }, 4000, "easeInOut" );
t1.animate({ 'width': 50, 'height': 50, 'x': 50*i + 28, 'y': 58 , 'title' : arr[i]}, 4000, "easeInOut" );
await sleep(time);
await sleep(time);
//sleep(2000);
document.getElementById("l21").className = "unhighl";
temp = arr[i];
arr[i] = min;
arr[index] = temp;
locations[i] = b ;
locations[index] = a ;
bb.attr({
fill : "white",
});
t.attr({
fill : "white",
});
await sleep(time);
//sleep(1);
}
//return sorted arr
return arr;
}
function ss(list){
//const list = [2,4,7,8] ;
paper = Raphael("sim", window.innerWidth , window.innerHeight);
var location = paper.set();
function Item(elem, text) {
this.elem = elem;
this.text = text;
}
var item ;
for(i=0;i<list.length;i++){
//making blocks for the very first time...
item = new Item(
paper.rect(50*i,30, 50,50),
//adding text separately ... coz theres no parent child relation in raphael ///
paper.text(50*i+28, 30+28 , list[i]).attr({
"font-weight": "bold" ,
'font-size': '35px'
}) );
locations[i] = item;
}
location = paper.set();
//user can select time
console.log(Selection_Sort(list, function(a, b) { return b - a; }));
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
var time = 1000;
var paper;
var rect;
var locations = [];