-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathordenando.js
More file actions
114 lines (97 loc) · 3.2 KB
/
ordenando.js
File metadata and controls
114 lines (97 loc) · 3.2 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
const list = document.getElementById("valores");
const entrada = document.getElementById("valor");
const vetor = [];
function add() {
let elemento = document.createElement("li");
let textEntrada = document.createTextNode(entrada.value);
elemento.appendChild(textEntrada);
list.appendChild(elemento);
let num = Number(entrada.value);
vetor.push(num);
}
function ordenar() {
let novalista = vetor.sort(function (a, b) {
if (a < b) {
return -1;
} else {
true;
}
});
let novoconteudo = novalista.map(elemento => `<li>${elemento}</li>`);
list.innerHTML = novoconteudo.join('');
document.body.appendChild(list);
}
function misturar() {
// Converte os valores da lista em um vetor
const arrayValores = Array.from(list.children).map(item => parseFloat(item.textContent));
// Embaralha o vetor
shuffle(arrayValores, arrayValores.length * 2);
// Atualiza a lista com os valores embaralhados
while (list.firstChild) {
list.removeChild(list.firstChild);
}
arrayValores.forEach(valor => {
const novoItem = document.createElement('li');
novoItem.textContent = valor;
list.appendChild(novoItem);
});
}
// Função para trocar os valores de duas posições em um vetor
const swap = (vetor, position1, position2) => {
const temp = vetor[position1];
vetor[position1] = vetor[position2];
vetor[position2] = temp;
};
// Função para embaralhar os elementos de um vetor
const shuffle = (vetor, numberOfSwaps) => {
for (let i = 0; i < numberOfSwaps; i++) {
const randomIndex1 = Math.floor(Math.random() * vetor.length);
const randomIndex2 = Math.floor(Math.random() * vetor.length);
swap(vetor, randomIndex1, randomIndex2);
}
};
// Função para ordenar um vetor com o algoritmo Bubble Sort
const bubble_sort = (vetor) => {
const n = vetor.length;
let swapped;
do {
swapped = false;
for (let i = 0; i < n - 1; i++) {
if (vetor[i] > vetor[i + 1]) {
swap(vetor, i, i + 1);
swapped = true;
}
}
} while (swapped);
};
// Função para encontrar o índice do menor elemento em um vetor
const findIndexOfMinimum = (vetor, startIndex) => {
let minIndex = startIndex;
for (let i = startIndex + 1; i < vetor.length; i++) {
if (vetor[i] < vetor[minIndex]) {
minIndex = i;
}
}
return minIndex;
};
// Função para ordenar um vetor com o algoritmo Quick Sort
const quick_sort = (vetor, startIndex, endIndex) => {
if (startIndex < endIndex) {
const pivotIndex = partition(vetor, startIndex, endIndex);
quick_sort(vetor, startIndex, pivotIndex - 1);
quick_sort(vetor, pivotIndex + 1, endIndex);
}
};
// Função de apoio ao Quick Sort para fazer o particionamento
const partition = (vetor, startIndex, endIndex) => {
const pivotValue = vetor[endIndex];
let pivotIndex = startIndex;
for (let i = startIndex; i < endIndex; i++) {
if (vetor[i] < pivotValue) {
swap(vetor, i, pivotIndex);
pivotIndex++;
}
}
swap(vetor, pivotIndex, endIndex);
return pivotIndex;
};