Created
September 18, 2018 16:26
-
-
Save asta-kun/3c21b7e1917b7a654f89251617a46dd9 to your computer and use it in GitHub Desktop.
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
package com.search.binary; | |
import java.util.Scanner; | |
import java.util.Stack; | |
public class Main { | |
public static int busquedaBinaria(int vector[], int dato){ | |
int n = vector.length;//longitud | |
int centro,inf=0,sup=n-1; | |
while(inf<=sup){//sup es el length (contando el 0) | |
centro=(sup+inf)/2; | |
if(vector[centro]==dato) return centro;//valor encontrado | |
else if(dato < vector [centro] ){ | |
//no encontrado pero el valor se encuentra en el lado "izquierdo" | |
sup=centro-1; | |
} | |
else { | |
//lado "derecho" | |
inf=centro+1; | |
} | |
} | |
//no hay resultados :c | |
return -1; | |
} | |
private static Stack<Integer> bubble (Stack<Integer> list) { | |
//ordena por burbuja | |
boolean swap = true;//flag | |
int temp; | |
while(swap){ | |
//seguira hasta que la bandera no existan cambios | |
swap = false; | |
for(int i = 0;i < list.size()-1; i++){ | |
if(list.get(i) > list.get(i+1)){ //comparacion | |
//se cambia elemento | |
temp = list.get(i); //toma el temporal para asignarlo una posicion mas adelante | |
list.set(i, list.get(i+1) );//donde se encontrada temp fue sustituido por su sucesor. | |
list.set(i+1, temp);//temp toma el lugar del sucesor | |
swap = true; //existe un cambio (vuelve a dar otra vuelta) | |
} | |
} | |
} | |
return list;//retorna lista ordenada | |
} | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
boolean status_menu = true; | |
int selected, index; | |
int aux; | |
int items_int[]; | |
Stack<Integer> items = new Stack<Integer>(); | |
//menu | |
do{ | |
System.out.println("########## MENU #############"); | |
System.out.println("1.- Agregar\n2.- Buscar\n3.- Mostrar todo\n4.- Salir\n>>>> Opción: #"); | |
selected = sc.nextInt(); | |
switch (selected){ | |
case 1: | |
//agregar | |
System.out.print("\n\n >>> Ingrese algun valor numerico:"); | |
items.add(sc.nextInt());//agregado | |
//ordenar | |
items = bubble(items); | |
break; | |
case 2: | |
//buscar | |
System.out.print("\n\nnumero a buscar:"); | |
aux = sc.nextInt(); | |
//pasar a array normal | |
items_int = items.stream().mapToInt(i->i).toArray();; | |
aux = busquedaBinaria(items_int, aux); | |
if(aux == -1){ | |
//no encontrado | |
System.out.println(">>>> No encontrado :c"); | |
}else{ | |
//encontrado | |
System.out.println(">>>> Encontrado, posicion #"+aux); | |
} | |
break; | |
case 3: | |
//mostrar todo | |
index = 0; | |
for(int temp: items){ | |
System.out.println("lugar #"+index + " ---> " +temp); | |
index+=1; | |
} | |
break; | |
case 4: | |
//salir | |
status_menu = false; | |
break; | |
default: | |
//no valida | |
System.out.println("Opción no valida....\n\n\n\n"); | |
} | |
}while(status_menu); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment