-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
71 lines (68 loc) · 1.97 KB
/
javascript.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
function animatedBubbleSort(bubble, selection, drawBubble, drawSelection) {
var i = 0;
var j = 0;
var k = 0;
var l = 0;
var steps = 0;
var length = bubble.length;
(function selectionSort() {
if (l >= length - k - 1) {
l = 0;
k++;
}
if (k < length) {
if (selection[l] > selection[l+1]) {
// swap itens
var temp = selection[l];
selection[l] = selection[l+1];
selection[l+1] = temp;
drawSelection(selection, l+1);
}
l++;
setTimeout(bubbleSort, 100);
} else //fim
drawSelection(selection);
})();
(function bubbleSort() {
if (j >= length - i - 1) {
j = 0;
i++;
}
if (i < length) {
if (bubble[j] > bubble[j+1]) {
// swap itens
var temp = bubble[j];
bubble[j] = bubble[j+1];
bubble[j+1] = temp;
drawBubble(bubble, j+1);
}
j++;
setTimeout(selectionSort, 100);
} else //fim
drawBubble(bubble);
})();
}
function ordenaRandom(){
var bubble = [];
var selection = [];
for (var i = 0; i < 20; i++) {
bubble.push(Math.floor((Math.random() * 100) + 1));
}
selection = bubble;
animatedBubbleSort(bubble, selection, function draw(bubble, j) {
document.getElementById("bubble-sort").innerHTML = bubble.map(function(v, i) { return i===j ? "<span>" + v + "</span>" : v; }).join(", ");
}, function draw(selection, l) {
document.getElementById("selection-sort").innerHTML = selection.map(function(s, k) { return k===l ? "<span>" + s + "</span>" : s; }).join(", ");
});
}
function ordenaCustom(){
var listAux = document.getElementById("orderingVector").value.split(",");
if(listAux[0] === ""){
return alert("Favor inserir números no campo de texto");
}
var list = [];
for (var i = 0; i < listAux.length; i++) {
list.push(parseInt(listAux[i]));
}
}
//alert("Você pode escolher entre ordenar uma lista de valores randômicos, ou uma lista com elementos de sua escolha!");