Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ejercicio7;
- import java.util.*;
- public class Ejercicio7 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int [] array = {2,3,4,5,6,7,8,9,10,11,12};
- int izq=0;
- int der= array.length-1;
- int centro=(izq+der)/2;
- int dato;
- System.out.print("Introduce número el número que deseas buscar: ");
- dato = sc.nextInt();
- System.out.print("Array: ");
- for (int i = 0; i < array.length; i++) {
- System.out.print(array[i]+" ");
- }
- System.out.println(" ");
- while(izq<=der && array[centro]!=dato){
- if (dato<array[centro]) {
- der =centro-1;
- }else
- izq = centro +1;
- centro=(izq+der)/2;
- }
- if (izq>der) {
- System.out.println("El elemento no se encuentra en el array.");
- }else
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement