-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortingTest.html
310 lines (291 loc) · 13.7 KB
/
SortingTest.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
309
310
<!DOCTYPE html>
<html>
<head>
<title>Sorting Test</title>
<link rel="stylesheet" href="main.css">
<meta charset="utf-8" />
</head>
<body>
<div>
<div id="postavka" class="postavka">
<h1>Sorting Test</h1>
<h4><a href="www.codewars.com">CodeWars</a> TwiceLinear test Kata lvl.5</h4>
<p>Consider a sequence u where <strong>u</strong> is defined as follows:</p>
<p>
<li>The number <strong>u(0) = 1</strong> is the first one in <strong>u</strong>.</li>
<li>For each <strong>x</strong> in <strong>u</strong>, then <strong>y = 2 * x + 1</strong> and <strong>z = 3 * x + 1</strong> must be in <strong>u</strong> too.</li>
<li>There are no other numbers in <strong>u</strong>.</li>
</p>
<p>Ex: <strong>u = [1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...]</strong> 1 gives 3 and 4, then 3 gives 7 and 10, 4 gives 9 and 13, then 7 gives 15 and 22 and so on...</p>
<p>Given parameter <strong>n</strong> the function <strong>dblLinear</strong> returns the element <strong>u(n)</strong> of the ordered (with min...max) sequence <strong>u</strong>.</p>
<ul>
<li>dbl_linear(10) should return 22</li>
<li>Focus attention on efficiency.</li>
</ul>
</div>
</div>
<div>
<div id="unos" class="unos">
Unesi brojeve razdvojene zarezom (,): <input id="poljeZaUnos" onkeypress="enterGo(event)"><button onclick="clickGo()">GO!</button> <button onclick="clickRandom()">Insert Random</button>
</div>
<div id="testVrednosti" class="testVrednosti">
<h4>Neke generisane vrednosti (poslednje 3):</h4>
<ul id="listaVrednosti" class="listaVrednosti">
<!-- <li id="test1...test5"></li> -->
</ul>
<div class="fadeoutVrednosti"></div>
</div>
<div id="resenja" class="resenja">
<p>Uneti podatak je: <span id="unetiPodatak"></span></p>
<p>Rezultat je: <span id="resultat"></span>, vreme za obradu : <span id="vreme"></span></p>
<div class="fadeoutResenja"></div>
</div>
<div id="liste" class="liste">
<p>Obradjene liste:</p>
<div class="fadeoutResenja"></div>
</div>
</div>
</body>
<script>
var brTestova = 0;
// Prva verzija koju sam napisao. Mnooogo je spora i ne pristupa problemu na pravi nacin.
function dblLinearSlow(n) {
if (n >= 1500) return -1; // znaci da je mnooogo sporo
var u = [1],
t = [],
i = 0,
counter = 0,
walker = 0,
oldn = 0,
oldern = 0;
for (i = 1; i < n; i += 1) {
u.push(2 * u[i - 1] + 1);
u.push(3 * u[i - 1] + 1);
oldern = oldn;
oldn = u[n];
//Bubble Sort samo drugu polovinu niza
for (counter = Math.floor(u.length / 2); counter < u.length - 1; counter += 1) {
for (walker = counter + 1; walker < u.length; walker += 1) {
if (u[walker] === u[counter]) {
u.splice(walker, 1);
continue;
};
if (u[walker] < u[counter]) {
t[0] = u[counter];
u[counter] = u[walker];
u[walker] = t[0];
};
};
};
if (u.length >= n + 1) {
if ((u[n] === oldn) && (u[n] === oldern)) break;
};
};
document.getElementById("unetiPodatak").innerHTML = document.getElementById("poljeZaUnos").value;
var tmpEl = document.createElement("P");
var tekst = document.createTextNode("Bubble Sort:".concat(u.join(", ")));
tmpEl.appendChild(tekst);
document.getElementById("liste").appendChild(tmpEl);
if (document.getElementById("liste").style.display != "block") document.getElementById("liste").style.display = "block";
return u[n];
};
// Druga verzija. Mnooogo je brza od prve ali i dalje mislim da nije pravi pristup.
function dblLinearFast(n) {
var index = 0,
u = [1],
oldn = 0,
oldern = 0;
for (index = 1; index < n; index += 1) {
u.push(2 * u[index - 1] + 1);
u.push(3 * u[index - 1] + 1);
oldern = oldn;
oldn = u[n];
// console.log(index + " - " + u + " - " + u.length + "elemenata");
for (var i = u.length - 3; i > 0; i -= 1) {
if (u[i] === u[u.length - 1]) {
u.splice(u.length - 1, 1);
if (u[i] === u[u.length - 1]) u.splice(u.length - 1, 1);
break;
};
if (u[i] === u[u.length - 2]) {
u.splice(u.length - 2, 1);
break;
};
};
//Quick Sort samo drugu polovinu niza
var newU = (function quickSort(list, left, right) {
var i = left,
j = right;
// definisi nasumicno mesto preloma niza za sortiranje
var mestoPreloma = left + Math.floor(Math.random() * (right + 1 - left));
var x = list[mestoPreloma];
// pronadji elemente za zamenu tako da su manji na levoj a veci na desnoj strani
while (i <= j) { // kada i===j, treba uporediti da bi se utvrdilo da li prebaciti levo ili desno
while (list[i] < x) {
i++;
}; // dok je tacno ne radi nista element je u pravoj grupi
while (x < list[j]) {
j--;
}; // dok je tacno ne radi nista element je u pravoj grupi
if (i < j) { // ovde smo ako je list[i] ili list[j] ili oba van redosleda
var temp = list[i];
list[i] = list[j];
list[j] = temp;
i++;
j--;
} else if (i === j) { // u ovom slucaju ne treba zamena
i++;
j--;
};
};
// Sada bi trebalo da je sve od left do j manje ili jednako prelomnoj vrednosti
// a sve od i do right da je vece ili jednako prelomnoj vrednosti.
if (left < j) {
quickSort(list, left, j);
};
if (i < right) {
quickSort(list, i, right);
};
return list;
})(u, Math.ceil(u.length / 2), u.length - 1);
if (newU.length >= n + 1) {
if ((newU[n] === oldn) && (newU[n] === oldern)) break;
};
};
document.getElementById("unetiPodatak").innerHTML = document.getElementById("poljeZaUnos").value;
var tmpEl = document.createElement("P");
var tekst = document.createTextNode("Quick Sort:".concat(newU.join(", ")));
tmpEl.appendChild(tekst);
document.getElementById("liste").appendChild(tmpEl);
if (document.getElementById("liste").style.display != "block") document.getElementById("liste").style.display = "block";
return u[n];
};
// Treca verzija. Najbrza je i izgleda da je tu negde pravi put.
function dblLinearRev(n) {
var u = [1],
t = [],
i = 0,
counter = 0,
oldn = 0,
oldern = 0,
nasao1 = 0,
nasao2 = 0;
for (i = 1; i < n; i += 1) { // osnovna petlja ide max do n mada se uvek prekida u daljem kodu
u.push(2 * u[i - 1] + 1);
u.push(3 * u[i - 1] + 1);
oldern = oldn;
oldn = u[n];
nasao1 = 0;
nasao2 = 0;
//Sort samo kraj niza od kraja, maximalno do polovine za svaki slucaj
// sa proverom duplikata.
for (counter = u.length - 3; counter > Math.floor(u.length / 2); counter -= 1) {
if ((counter < 0) || (nasao1 && nasao2)) break;
if (!nasao1 && (u[u.length - 2] < u[counter]) && (u[u.length - 2] > u[counter - 1])) {
u.splice(counter, 0, u[u.length - 2]);
u.splice(u.length - 2, 1);
nasao1 = 1;
if (nasao1 && nasao2) break;
} else if (!nasao1 && (u[u.length - 2] === u[counter])) {
u.splice(u.length - 2, 1);
nasao1 = 1;
if (nasao1 && nasao2) break;
};
if (!nasao2 && (u[u.length - 1] < u[counter]) && (u[u.length - 1] > u[counter - 1])) {
u.splice(counter, 0, u[u.length - 1]);
u.splice(u.length - 1, 1);
nasao2 = 1;
if (nasao1 && nasao2) break;
} else if (!nasao2 && (u[u.length - 1] === u[counter])) {
u.splice(u.length - 1, 1);
nasao2 = 1;
if (nasao1 && nasao2) break;
};
};
if (u.length >= n + 1) {
if ((u[n] === oldn) && (u[n] === oldern)) break;
};
}; // end osnovne petlje
var tmpEl = document.createElement("P");
var tekst = document.createTextNode("Reverse Sort:".concat(u.join(", ")));
tmpEl.appendChild(tekst);
document.getElementById("liste").appendChild(tmpEl);
if (document.getElementById("liste").style.display != "block") document.getElementById("liste").style.display = "block";
return u[n];
};
function Ivan(n) {
var u = [1],
i = 0;
for (i; i < n - 1; i += 1) {
u = insertToArray(u, 2 * u[i] + 1);
u = insertToArray(u, 3 * u[i] + 1);
}
var tmpEl = document.createElement("P");
var tekst = document.createTextNode("Ivan Sort:" + u.join(", "));
tmpEl.appendChild(tekst);
document.getElementById("liste").appendChild(tmpEl);
return u[n];
}
function insertToArray(a, d) {
var l = a.length;
for (l; l > 0; l -= 1) {
if (a[l - 1] === d) {
break;
}
if (a[l - 1] < d) {
a.splice(l, 0, d);
break;
}
}
return a;
}
function clickGo() {
var unetiString = function cleanup(str) {
return (str.trim().charAt(str.trim().length - 1) === ",") ? cleanup(str.trim().slice(0, str.trim().length - 1)) : str.trim();
}(document.getElementById("poljeZaUnos").value);
document.getElementById("poljeZaUnos").value = unetiString;
/* var vremePocetka = Date.now();
document.getElementById("resultat").innerHTML = "Bubble Sort - ".concat(dblLinearSlow(Number(unetiString)));
var vremeKraja = Date.now();
document.getElementById("vreme").innerHTML = "Bubble Sort - ".concat(((vremeKraja - vremePocetka) / 1000 < 1)?(vremeKraja - vremePocetka)+"ms":((vremeKraja - vremePocetka) / 1000)+"s");
var vremePocetka = Date.now();
document.getElementById("resultat").innerHTML = document.getElementById("resultat").innerHTML.concat(" | Quick Sort - ", dblLinearFast(Number(unetiString)));
var vremeKraja = Date.now();
document.getElementById("vreme").innerHTML = document.getElementById("vreme").innerHTML.concat(" | Quick Sort - ", ((vremeKraja - vremePocetka) / 1000 < 1)?(vremeKraja - vremePocetka)+"ms":((vremeKraja - vremePocetka) / 1000)+"s");
var vremePocetka = Date.now();
document.getElementById("resultat").innerHTML = document.getElementById("resultat").innerHTML.concat(" | Reverse Sort - ", dblLinearRev(Number(unetiString)));
var vremeKraja = Date.now();*/
var vremePocetka = Date.now();
document.getElementById("resultat").innerHTML = document.getElementById("resultat").innerHTML + (" | Ivan Sort - " + Ivan(Number(unetiString)));
var vremeKraja = Date.now();
document.getElementById("vreme").innerHTML = document.getElementById("vreme").innerHTML.concat(" | Reverse Sort - ", ((vremeKraja - vremePocetka) / 1000 < 1) ? (vremeKraja - vremePocetka) + "ms" : ((vremeKraja - vremePocetka) / 1000) + "s");
document.getElementById("resenja").style.maxHeight = "220px";
document.getElementById("resenja").style.display = "block";
document.getElementById("resenja").scrollIntoView();
};
function enterGo(event) {
if (event.keyCode === 13) clickGo();
};
function clickRandom() {
brTestova++;
/*
var gen = "", tmpArr = [];
for(var N = 2; N <= Math.floor(Math.random()*100000+1); N+=1 ){
tmpArr.push(Math.floor(Math.random()*2000+1)-1000); };
gen = tmpArr.join(", "); */
gen = Math.floor(Math.random() * 69999 + 1);
document.getElementById("poljeZaUnos").value = gen;
if (brTestova > 3) {
document.getElementById("test1").innerHTML = document.getElementById("test2").innerHTML;
document.getElementById("test2").innerHTML = document.getElementById("test3").innerHTML;
document.getElementById("test3").innerHTML = gen;
} else {
var noviElement = document.createElement("LI");
noviElement.id = "test" + brTestova;
document.getElementById("listaVrednosti").appendChild(noviElement);
document.getElementById("test" + brTestova).innerHTML = gen;
document.getElementById("testVrednosti").style.display = "block";
};
};
</script>
</html>