How to apply binary search with String when reading a txt?

0

Good friends, you will see that I have to read a txt file with data "name", "phone" and then perform the binary search of a name in the.

I have already read the txt file and separated the data in the commas, however I do not understand how I can send the array with the separated values to the method that will perform the search.

  

This is what I have in my main class

using System;
using System.IO;
using System.Collections;

namespace busquedaAgenda
{
    class Agenda
    {
        //Arreglo que tendrá los datos separados
        public static ArrayList Registro = new ArrayList();

        //Método para separar los datos del archivo
        public void leer(String nomBusca)
        {
            string linea;       //Cada linea de texto
            String[] separar;   //Arreglo que tendrá por separados los datos
            Contacto cont = new Contacto(); //objeto que servirá para ir guardando los registros

            StreamReader archivo = new StreamReader("Contactos.txt");
            while ((linea = archivo.ReadLine()) != null)
            {
                //Separar mediante comas
                separar = linea.Split(',');

                //Se establece el orden y los datos que se guardaran
                cont.Nombre = separar[0];
                cont.Telefono = separar[1];

                //Se guarda en el registro
                Registro.Add(cont);


            }
            archivo.Close();

            //Llamar método que realizará la busqueda binaria
            Contacto.busquedaBinaria(Registro, nomBusca);
        }

        static void Main(string[] args)
        {
            Agenda nuevo = new Agenda();
            String nombre;

            Console.WriteLine("\tAgenda telefónica\n");

            //solicuta nombre a buscar
            Console.Write("\nContacto a buscar: ");
            nombre = Console.ReadLine().ToUpper();

            //Enviar el nombre al método que leerá los datos
            nuevo.leer(nombre);

            //Contacto.busquedaBinaria(Registro, nombre);

        }
    }
}
  

This is my other class that has the method to perform the binary search

using System.Collections;
using System;

namespace busquedaAgenda
{
    class Contacto
    {
        //Propiedades autoimplementadas
        public String Nombre { get; set; }
        public String Telefono { get; set; }

        public Contacto()
        {
            this.Nombre = Nombre;
            this.Telefono = Telefono;
        }

        public static void busquedaBinaria(ArrayList datos, string nomBuscar)
        {
            int izq, der, centro;
            izq = 0;
            der = datos.Count - 1;
            centro = (izq + der) / 2;

            Console.WriteLine(datos[centro]);
            while (nomBuscar != datos[centro] && izq < der)
            {
                if (nomBuscar > datos[centro])
                {
                    izq = centro + 1;
                }
                if (nomBuscar < datos[centro])
                {
                    der = centro - 1;
                }
                centro = (izq + der) / 2;
            }
            if (nomBuscar == datos[centro])
            {
                Console.WriteLine($"El número {nomBuscar} se encuentra en la posición {centro}");
            }
            else
            {
                Console.WriteLine($"El número {nomBuscar} no se encuentra");
            }
        }
    }

}

If someone were kind enough to tell me how to send the data from the Registry or a better way to separate the data, I would appreciate it.

    
asked by Manuel Hdez Galván 15.04.2017 в 00:59
source

2 answers

1

You can use System.Collections.Generic; You must be careful when you use a list in a cycle, for each element you add you must create an instance. To compare string you can use the CompareTo method

using System;
using System.Collections.Generic;
using System.IO;
class Agenda
{
    //Arreglo que tendrá los datos separados
    List<Contacto> Registro = new List<Contacto>();

    //Método para separar los datos del archivo
    public void leer(String nomBusca)
    {
        string linea;       //Cada linea de texto
        String[] separar;   //Arreglo que tendrá por separados los datos

        StreamReader archivo = new StreamReader("Contactos.txt");
        while ((linea = archivo.ReadLine()) != null)
        {
            Contacto cont = new Contacto();
            //Separar mediante comas
            separar = linea.Split(',');

            //Se establece el orden y los datos que se guardaran
            cont.Nombre = separar[0];
            cont.Telefono = separar[1];

            //Se guarda en el registro
            Registro.Add(cont);


        }
        archivo.Close();

        //Llamar método que realizará la busqueda binaria
        Contacto.busquedaBinaria(Registro, nomBusca);
    }

    class Contacto
    {
        //Propiedades autoimplementadas
        public String Nombre { get; set; }
        public String Telefono { get; set; }

        public Contacto()
        {
            this.Nombre = Nombre;
            this.Telefono = Telefono;
        }

        public static void busquedaBinaria(List<Contacto> datos, string nomBuscar)
        {
            int izq, der, centro;
            izq = 0;
            der = datos.Count - 1;
            centro = (izq + der) / 2;

            Console.WriteLine(datos[centro].Nombre);
            while (nomBuscar != datos[centro].Nombre && izq < der)
            {
                int comparacion = nomBuscar.CompareTo(datos[centro].Nombre);

                if (comparacion > 0)
                {
                    izq = centro + 1;
                }
                if (comparacion < 0)
                {
                    der = centro - 1;
                }
                centro = (izq + der) / 2;
            }
            if (nomBuscar == datos[centro].Nombre)
            {
                Console.WriteLine("El número {0} se encuentra en la posición {1}", nomBuscar, centro);
            }
            else
            {
                Console.WriteLine("El número {0} no se encuentra", nomBuscar);
            }
        }
    }
}
    
answered by 15.04.2017 / 05:15
source
0

If also the problem as demonstrated by the previous answer is that you were comparing a datos[centro] object against a string:

  

error ---> while (nomBuscar! = data [center] & < der)

Below, the name of the object is compared against the searched name.

  

okey --- > while (nomBuscar! = data [centro] .Name & left

answered by 25.04.2017 в 17:29