-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrdenarCandidatos.java
More file actions
74 lines (69 loc) · 2.49 KB
/
OrdenarCandidatos.java
File metadata and controls
74 lines (69 loc) · 2.49 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
package Etapa2;
/**
* Algoritmos de ordenação (estáveis) e pesquisa binária para Candidato[].
*
* Observação: usamos insertion sort para garantir ESTABILIDADE.
* - Por Nome : ordem alfabética (A..Z), ignorando caixa.
* - Por Partido : ordem alfabética (A..Z), ignorando caixa.
* - Por Votos : ordem decrescente (maior -> menor).
*/
public class OrdenarCandidatos {
public static void ordenaCandidatosPorNome(Candidato[] v) {
for (int i = 1; i < v.length; i++) {
Candidato chave = v[i];
int j = i - 1;
while (j >= 0 && v[j].getNome().compareToIgnoreCase(chave.getNome()) > 0) {
v[j + 1] = v[j];
j--;
}
v[j + 1] = chave; // estável
}
}
public static void ordenaCandidatosPorPartido(Candidato[] v) {
for (int i = 1; i < v.length; i++) {
Candidato chave = v[i];
int j = i - 1;
while (j >= 0 && v[j].getPartido().compareToIgnoreCase(chave.getPartido()) > 0) {
v[j + 1] = v[j];
j--;
}
v[j + 1] = chave; // estável
}
}
public static void ordenaCandidatosPorVotos(Candidato[] v) {
// decrescente: maior para menor
for (int i = 1; i < v.length; i++) {
Candidato chave = v[i];
int j = i - 1;
while (j >= 0 && v[j].getIntencoesVotos() < chave.getIntencoesVotos()) {
v[j + 1] = v[j];
j--;
}
v[j + 1] = chave; // estável
}
}
/**
* Pesquisa binária por NOME em um vetor JÁ ORDENADO POR NOME (A..Z).
* @param v vetor ordenado por nome
* @param nome nome a procurar
* @return posição encontrada (primeira ocorrência), ou -1 se não existir
*/
public static int pesquisaBinariaCandidatos(Candidato[] v, String nome) {
int ini = 0, fim = v.length - 1;
int pos = -1;
while (ini <= fim) {
int meio = (ini + fim) >>> 1;
int cmp = v[meio].getNome().compareToIgnoreCase(nome);
if (cmp == 0) {
pos = meio;
// opcional: voltar para a primeira ocorrência entre duplicados
fim = meio - 1;
} else if (cmp < 0) {
ini = meio + 1;
} else {
fim = meio - 1;
}
}
return pos;
}
}