Está en la página 1de 5

FACULTAD DE INGENIERÍA

CARRERA DE INGENIERÍA DE SISTEMAS COMPUTACIONALES

RESOLUCION DE EJERCICIOS
SEMANA14

BUSQUEDA LINEAL - BINARIA

Curso:
Fundamentos de Programación

Docente:
Javier Miguel Rosales Guerra

LIMA – PERÚ

2022-1
SEMANA14

PRACTICA 14 – BUSQUEDA LINEAL Y BINARIA

1. OBJETIVOS

Implementar aplicaciones para realizar búsquedas en estructuras de datos.

2. EQUIPOS Y MATERIALES

Visual Studio

IMPLEMENTACIÓN EN FORMULARIOS DE WINDOWS

2
Página

FUNDAMENTOS DE PROGRAMACION
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProyectoBusquedaLinealBinaria_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int[] vector1, vector2;
Random random = new Random();

private void btnBuscar1_Click(object sender, EventArgs e)


{
int sw = 0;
int dato = int.Parse(txtDato1.Text);
for(int i = 0; i < vector1.Length; i++)
{
if (dato == vector1[i])
{
txtResult1.Text += "El dato " + dato + " fue encontrado en la
pos." + i + "\n";
if (dato % 2 == 0)
txtResult1.Text += "El dato " + dato + " es par\n";
else
txtResult1.Text += "El dato " + dato + " es impar\n";
sw = 1;
listBox1.SelectedIndex = i;
}
}
if(sw==0)
MessageBox.Show("El dato "+dato+" NO fue
encontrado","Alerta");
}

private void btnGenerar2_Click(object sender, EventArgs e)


{
int n = int.Parse(txtNro2.Text);
vector2 = new int[n];
listBox2.Items.Clear();
for (int i = 0; i < vector2.Length; i++)
{
vector2[i] = random.Next(1, 101);
3

}
Página

ordenarVector(vector2);
for (int i = 0; i < vector2.Length; i++)

FUNDAMENTOS DE PROGRAMACION
{
listBox2.Items.Add("[" + i + "] <= " + vector2[i]);
}
}
private void ordenarVector(int[] vector)
{
int aux;
for(int i = 0; i < vector.Length; i++)
{
for(int j = i + 1; j < vector.Length; j++)
{
if (vector[i] > vector[j])
{
aux = vector[i];
vector[i] = vector[j];
vector[j] = aux;
}
}
}
}

private void btnBuscar2_Click(object sender, EventArgs e)


{
int dato = int.Parse(txtDato2.Text);
int l = 0, h = vector2.Length;
int m = 0;
bool encontrado = false;
while(l<=h && encontrado == false)
{
m = (l + h) / 2;
if (vector2[m] == dato)
encontrado = true;
if (vector2[m] > dato)
h = m - 1;
else
l = m + 1;
}
if(encontrado==false)
MessageBox.Show("El dato "+dato+" NO se encuentra","Alerta",
MessageBoxButtons.OK,MessageBoxIcon.Information);
else
{
txtResult2.Text += "El dato " + dato + " fue encontrado en la
pos." + m + "\n";
if (dato % 2 == 0)
txtResult2.Text += "El dato " + dato + " es par\n";
else
txtResult2.Text += "El dato " + dato + " es impar\n";
listBox2.SelectedIndex = m;
}
}

private void btnGenerar1_Click(object sender, EventArgs e)


{
4

int n = int.Parse(txtNro1.Text);
Página

vector1 = new int[n];


listBox1.Items.Clear();

FUNDAMENTOS DE PROGRAMACION
for(int i = 0; i < vector1.Length; i++)
{
vector1[i] = random.Next(1, 101);
listBox1.Items.Add("[" + i + "] <= " + vector1[i]);
}
}
}
}

5
Página

FUNDAMENTOS DE PROGRAMACION

También podría gustarte