Advertisement
Zeill

Busqueda Secuencial

Oct 22nd, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. public class BusquedaLineal {
  2.    
  3.     public int busquedaSecuencial(char arr[], char buscar) {
  4.         int length = arr.length;
  5.         for (int i = 0; i < length; i++) {
  6.            
  7.             //Comparamos contra cada elemento del array
  8.             if (arr[i] == buscar) {
  9.                 return i;
  10.             }
  11.            
  12.         }
  13.         return -1;
  14.     }
  15.    
  16.     public static void main(String[] args) {
  17.        
  18.         char[] array = {'a','b','c','d','e'};
  19.        
  20.         BusquedaLineal buscar = new BusquedaLineal();
  21.        
  22.         int resultado = buscar.busquedaSecuencial(array, 'c');
  23.        
  24.         if (resultado != -1) {
  25.             System.out.println("El elemento fue encontrado en el índice: " + resultado);
  26.         } else {
  27.             System.out.println("No se encontró el elemento en el array");
  28.         }
  29.        
  30.     }
  31.    
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement