-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselection.js
More file actions
147 lines (96 loc) · 3.22 KB
/
Copy pathselection.js
File metadata and controls
147 lines (96 loc) · 3.22 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
async function Selection_Sort(arr, compare_Function) {
function compare(a, b) {
return b - a;
}
var min = 0;
var index = 0;
var temp = 0;
//{Function} compare_Function Compare function
compare_Function = compare_Function || compare;
for (var i = 0; i < arr.length; i += 1) {
// k = document.getElementById('sorted')
// document.getElementById('p2').innerHTML = '0 to'+Number(i);
// document.getElementById('p3').innerHTML = i+' to '+arr.length;
await sleep(time);
var a = locations[i] ;
var bb = locations[i].elem ;
var bb1 = locations[i].text ;
bb.attr({
fill : "red",
});
await sleep(time);
//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];
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 <
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);
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);
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);
}
//return sorted arr
return arr;
}
function ss(list){
//const list = [2,4,7,8] ;
paper = Raphael("container", 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': '40px'
}) );
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 = [];