Created
December 15, 2018 11:41
-
-
Save luscas/5524e8fb07a38d925d890a181a7c4b8c to your computer and use it in GitHub Desktop.
Busca Sequencial com Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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