This repository was archived by the owner on Aug 3, 2021. It is now read-only.
forked from techieshouvik/Sorting-Visualizer
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscript.js
202 lines (179 loc) · 7.02 KB
/
script.js
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
var generate = document.querySelector('.generate');
var technique = document.querySelector('.technique');
var run = document.querySelector('.run');
var visualizer = document.querySelector('.visualizer');
var visualizerContainer = document.querySelector('.visualizer-container');
visualizerContainer.innerHTML = "";
var arr = [100];
var counter = 0;
var swap;
//Generate Once at Program Start
visualizer.innerHTML = "";
for(let i=0;i<100;i++){
arr[i] = Math.floor((Math.random() * 100) + 1);
const obj = document.createElement('div');
obj.classList.add('obj');
obj.value = {val:arr[i],index:i};
obj.style.height = obj.value.val+"%";
visualizer.appendChild(obj);
}
visualizerContainer.appendChild(visualizer);
const len = arr.length;
generate.addEventListener('click', function(){
console.clear();
visualizer.innerHTML = "";
for(let i=0;i<100;i++){
arr[i] = Math.floor((Math.random() * 100) + 1);
const obj = document.createElement('div');
obj.classList.add('obj');
obj.value = {val:arr[i],index:i};
obj.style.height = obj.value.val+"%";
visualizer.appendChild(obj);
}
visualizerContainer.appendChild(visualizer);
});
run.addEventListener('click', function(e){
e.preventDefault();
const sorter = technique.value;
console.log(sorter);
let v = 0;
switch (sorter){
case "bubble":
generate.classList.add('run-running');
run.classList.add('run-running');
for (let i = 0; i < len; i++) {
counter++;
for (let j = 0; j < len; j++) {
setTimeout(function(){
if (arr[j] > arr[j+1]) {
let tmp1 = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp1;
visualizer.innerHTML = "";
for(let i=0;i<len;i++){
const obj = document.createElement('div');
obj.classList.add('obj');
obj.value = {val:arr[i],index:i};
obj.style.height = obj.value.val+"%";
obj.classList.add('running');
visualizer.appendChild(obj);
}
visualizerContainer.appendChild(visualizer);
}
pauseComp(0);
});
}
}
var m = setInterval(function(){
if(counter>=len-1){
visualizer.childNodes.forEach(function(node){
generate.classList.remove('run-running');
node.classList.remove('running');
node.classList.add("success");
run.classList.remove('run-running');
});
counter=0;
clearInterval(m);
}
},500);
break;
case "selection":
generate.classList.add('run-running');
run.classList.add('run-running');
for(let i = 0; i < len; i++) {
counter++;
// Finding the smallest number in the subarray
setTimeout(function(){
let min = i;
for(let j = i+1; j < len; j++){
if(arr[j] < arr[min]) {
min=j;
}
}
if (min != i) {
// Swapping the elements
let tmp = arr[i];
arr[i] = arr[min];
arr[min] = tmp;
visualizer.innerHTML = "";
for(let i=0;i<len;i++){
const obj = document.createElement('div');
obj.classList.add('obj');
obj.value = {val:arr[i],index:i};
obj.style.height = obj.value.val+"%";
obj.classList.add('running');
visualizer.appendChild(obj);
}
visualizerContainer.appendChild(visualizer);
}
pauseComp(60);
},0);
}
var m = setInterval(function(){
if(counter>=len-1){
visualizer.childNodes.forEach(function(node){
generate.classList.remove('run-running');
node.classList.remove('running');
node.classList.add("success");
run.classList.remove('run-running');
});
counter=0;
clearInterval(m);
}
},500);
break;
case "insertion":
generate.classList.add('run-running');
run.classList.add('run-running');
let n = arr.length;
for (let i = 1; i < n; i++) {
counter++;
setTimeout(function(){
let current = arr[i];
let j = i-1;
while ((j > -1) && (current < arr[j])) {
arr[j+1] = arr[j];
j--;
}
arr[j+1] = current;
visualizer.innerHTML = "";
for(let i=0;i<len;i++){
const obj = document.createElement('div');
obj.classList.add('obj');
obj.value = {val:arr[i],index:i};
obj.style.height = obj.value.val+"%";
obj.classList.add('running');
visualizer.appendChild(obj);
}
visualizerContainer.appendChild(visualizer);
pauseComp(60);
},0);
}
var m = setInterval(function(){
if(counter>=len-1){
visualizer.childNodes.forEach(function(node){
generate.classList.remove('run-running');
node.classList.remove('running');
node.classList.add("success");
run.classList.remove('run-running');
});
counter=0;
clearInterval(m);
}
},500);
break;
}
});
visualizer.childNodes.forEach(function(node){
node.addEventListener('click', function(){
alert(node.value.val);
});
});
function pauseComp(ms)
{
var curr = new Date().getTime();
ms += curr;
while (curr < ms) {
curr = new Date().getTime();
}
}