I've been stopped for a few days and I can't figure out what I'm missing in the binary search code.
You have to divide the array in two and search downwards or upwards if it is manor or greater than the number found in the middle of it
I have something in the code that without error leaves as an infinite loop
Somebody could help me ?
It is about generating two arrays and looking for the indicated number
static void Main(string[] args)
{
int I = 0;
int CAN = 0;
int J = 0;
int ALTO = 0;
int BAJO = 0;
int CENTRAL = 0;
int BUS = 0;
int AUX = 0;
string linea;
Random rnd = new Random();
Console.Write("CUANTOS ELEMENTOS:");
linea = Console.ReadLine();
CAN = int.Parse(linea);
int[] VEC = new int[CAN + 1];
bool ENCONTRADO = false;
// INGRESO
for (I = 1; I <= CAN; I++)
{
VEC[I] = rnd.Next(0, 99);
}
// SALIDA DEL ARREGLO ALEATORIO
for (I = 1; I <= CAN; I++)
{
Console.SetCursorPosition(3, I + 2); Console.WriteLine(VEC[I]);
} // PRIMERO ORDENAMOS EL ARREGLO
for (J = 1; J <= CAN; J++)
{
for (I = 1; I <= CAN - 1; I++)
{
if ((VEC[I] > VEC[I + 1]))
{
AUX = VEC[I];
VEC[I] = VEC[I + 1];
VEC[I + 1] = AUX;
}
}
}
// SALIDA DEL ARREGLO ORDENADO
for (I = 1; I <= CAN; I++)
{
Console.SetCursorPosition(10, I + 2);
Console.WriteLine(VEC[I]);
}
// AHORA SI LA BÚSQUEDA
Console.Write("ELEMENTO A BUSCAR:");
linea = Console.ReadLine();
BUS = int.Parse(linea);
BAJO = 1;
ALTO = CAN;
// CENTRAL = (BAJO + ALTO) / 2
while (((BAJO <= ALTO) & (ENCONTRADO == false)))
{
CENTRAL = (BAJO + ALTO) / 2;
if ((VEC[CENTRAL] == BUS))
{
ENCONTRADO = true;
}
}
if ((ENCONTRADO))
{
Console.WriteLine("{0} Encontrado en la posicion {1}", BUS, CENTRAL);
}
else
{
Console.WriteLine("No existe {0} ", BUS);
}
Console.Write("Pulse una Tecla:");
Console.ReadLine();
}
I check and check and find nothing that could cause that infinite loop, but the program stays with the black screen and does nothing
Ok, successful, I am missing an if that limited the searches
I'm glad you solved it for you, however here is a solution using C# 's own functions for this.
In C# there is the BinarySearch function of the Array class . This function has several overloads, here I only explain the one that receives two parameters: the first parameter is the array itself and the second would be the element to find within said array.
The function returns an integer that would be the index where the element sought in the array was found.
If the value returned by the function is less than zero it means that the element does not exist in the array, if the value returned is greater than zero then the element exists and the index of that element in the array is returned.
Since it is a binary search, the array must be sorted , so we must sort the array first.
To sort the array, the Sort function of the Array class is used, which receives the array to sort as a parameter (in this case I explain, since this function has several overloads with several parameters) and returns nothing ( void ).
So the code would look like this:
To use this function you must put in the usings: