abr 2, 2009

Enviado por en C/C++, Programacion

Busqueda binaria c++

Buenas..

Creo que no hay que describir mucho en que consiste el algoritmo de busqueda binaria, aclarar que a la función hay que pasarle el array de elementos ordenandos porque sino no funciona, y el otro parametro es el elemento a buscar.. esta funcion es con numeros enteros pero puede ser con cualquier tipo de dato siempre que esten ordenados.

int busquedaBinaria(int array[], int buscar)
{
      //MAX es mi constante del tamaño del arreglo pueden usar lenght
	int sup = MAX-1,
	    inf = 0,
	    centro;

	while(inf < sup)
	{
		centro = (sup+inf)/2;

		if(buscar == array[centro])
			return centro;

		else
			if(array[centro] > buscar)
				sup = centro - 1;

		else
			inf = centro + 1;
	}
}

    Posts Relacionados

    1. Que flojo… ordenalo de una vez!!!

      Tan facil que es!

      Usando Firefox 3.1b3 Firefox 3.1b3 en GNU/Linux GNU/Linux

    Menciones/Notificaciones

    1. mobile website dimensions - way. writing a title that target the... emotional hot spots of your target audience is exactly what you need to do ...
    2. depression testimony - little over three feet long and tend... to look more like a viper than a snake because they have a triangular ...

    Dejar una respuesta

    Debes ser Alojarse para enviar un comentario.