//buscar un numero.
#include <iostream>
using namespace std;
int main()
{
int vector[] = {1,2,3,4,5,6,7,8,9,10,11,12}; //vector ordenado
int tam = 11;
int buscar,trozo;
buscar = 2;
trozo = tam/2;
int temp = trozo,contador = 0,elemento = vector[trozo];
if(buscar == vector[0] or buscar == vector[tam]){
cout << "El Elemento existe" << endl;
}
else if(buscar < vector[0] or buscar > vector[tam]){
cout << "El elemento no existe" << endl;
}
else
{
while( contador <= tam)
{
if(buscar > vector[trozo-1] and buscar < vector[trozo+1] ){
contador = tam;
}
else if(buscar < elemento)
{
trozo -= temp/2;
elemento = vector[trozo];
}
else if(buscar > elemento){
trozo += temp /2 - 1;
elemento = vector[trozo];
temp = trozo;
}
else{
contador = tam;
}
contador++;
}
if(buscar == elemento){
cout << "Existe" << endl;
}
else{
cout << "No existe" << endl;
}
}
system("pause");
return 0;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.