Skip to content

Instantly share code, notes, and snippets.

@luscas
Created December 15, 2018 11:41
Show Gist options
  • Save luscas/5524e8fb07a38d925d890a181a7c4b8c to your computer and use it in GitHub Desktop.
Save luscas/5524e8fb07a38d925d890a181a7c4b8c to your computer and use it in GitHub Desktop.
Busca Sequencial com Java
import java.lang.String;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
public class Turma {
private List alunos = new ArrayList();
public Turma(String novoAluno) {
alunos.add(novoAluno);
}
public boolean buscaSequencial(String nomeDoAluno) { // Busca sequencial
for(Object aluno : alunos) {
if( alunos.equals(nomeDoAluno) ) {
return true;
}
}
return false;
}
public boolean buscaComContains(String nomeDoAluno) { // Busca com Contains
return alunos.contains(nomeDoAluno);
}
public boolean buscaBinaria(String nomeDoAluno) { // Lista ordenadas
int inicio = 0;
int fim = alunos.size() - 1;
while(inicio <= fim) {
int meio = fim / 2;
if( alunos.get(meio).equals(nomeDoAluno) ) {
return true;
//} else if( alunos.get(meio).compareTo(nomeDoAluno) < 0 ) {
// inicio = meio + 1; // Proximo
} else {
fim = meio - 1; // Anterior
}
}
return false;
}
public boolean buscaBinariaSearch(String nomeDoAluno) {
return Collections.binarySearch(alunos, nomeDoAluno) > -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment