-
Notifications
You must be signed in to change notification settings - Fork 24
issue-43-busca-linear #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Busca linear | ||
|
|
||
| ### Introdução | ||
|
|
||
| A busca linear consite em um método de busca através do qual percorremos um array de elementos comparando cada elemento com o valor que desejamos encontrar. | ||
| Esse tipo de busca é muito utilizado quando não sabemos a posição do elemento que desejamos encontrar. | ||
| Contudo, para a complexidade desse algoritmo é O(n), ou seja, o tempo de execução é proporcional ao tamanho do array. Em caso de arrays muito grandes, esse tipo de busca pode ser muito custoso. | ||
|
|
||
| ## Funcionamento do algoritmo | ||
| O algoritmo realiza as seguintes etapas: | ||
| 1. Cria um array com 5 posições. | ||
| 2. Preenche o array com valores inteiros. | ||
| 3. Solicita ao usuário que digite um valor que deseja encontrar. | ||
| 4. Percorre o array através deu um for comparando cada elemento com o valor que desejamos encontrar. | ||
| 5. Se o valor for encontrado, ele atribue o valor 'true' à variável isNumero, e atribui o valor encontrado à variável numeroLocalizado. | ||
| 6. Caso o valor não seja encontrado, ele atribui o valor 'false' à variável isNumero. | ||
| 7. Exibe o resultado da busca. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,33 @@ | ||||||||||||||||
| import java.util.Scanner; | ||||||||||||||||
|
|
||||||||||||||||
| public class Main { | ||||||||||||||||
| public static void main(String[] args) { | ||||||||||||||||
|
|
||||||||||||||||
| var sc = new Scanner(System.in); | ||||||||||||||||
|
|
||||||||||||||||
| int[] vetor = new int[5]; | ||||||||||||||||
| vetor[0] = 5; | ||||||||||||||||
| vetor[1] = 1; | ||||||||||||||||
| vetor[2] = 2; | ||||||||||||||||
| vetor[3] = 3; | ||||||||||||||||
| vetor[4] = 4; | ||||||||||||||||
|
|
||||||||||||||||
| boolean isNumero = false; | ||||||||||||||||
| int numeroLocalizado = 0; | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Vc pode remover essa variável pois consegue printar no final somente com o numeroBuscado |
||||||||||||||||
|
|
||||||||||||||||
| System.out.println("insira o numero a ser buscado: "); | ||||||||||||||||
| int numeroBuscado = sc.nextInt(); // criada uma variável pra armazenar o numero que será buscado pelo algoritmo | ||||||||||||||||
|
|
||||||||||||||||
| for (int i : vetor) { | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Para aumentar a legibilidade do for sugiro:
Suggested change
|
||||||||||||||||
| if (numeroBuscado == i) { | ||||||||||||||||
| isNumero = true; | ||||||||||||||||
| numeroLocalizado = i; | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Vc pode remover a numeroLocalizado aqui |
||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| if (isNumero) { | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Se vc quiser substituir o for e utilizar lambda é possível usar a seguinte expressão:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explicando cada parte: |
||||||||||||||||
| System.out.println("valor encontrado: " + numeroLocalizado); | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Substituir numeroLocalizado por numeroBuscado |
||||||||||||||||
| } else { | ||||||||||||||||
| System.out.println("valor nao encontrado: " + numeroBuscado); | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aqui você pode iniciar o array com int[] vetor = new int[]{5, 1, 2, 3, 4}; para maior legibilidade