0no

Pesquisa binária

0no
Nov 7th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.35 KB | None | 0 0
  1. // Método faz a pesquisa binária
  2.  
  3. public static int pesquisaBinaria(int x) {
  4.     int esq = 0;
  5.     int dir = vetor.length - 1;
  6.     int meio;
  7.     do {
  8.         meio = esq + (dir - esq) / 2;
  9.             if (x < vetor[meio]) {
  10.                 dir = meio - 1;
  11.             } else if (x > vetor[meio]) {
  12.                 esq = meio + 1;
  13.             } else {
  14.                 return 1;
  15.             }
  16.     } while (esq <= dir);
  17.     return 0;
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment