Binary search of strings in an array in C#

Cameron Briggs

Ok so I have been at this same error for about 18 hours and am totally lost. What I am trying to do is conduct a binary search where the search begins in the middle of the array and then eliminates half of the array everytime by comparing the term that was searched for to the middle term. So far my code is not producing errors except when I try to compare if the searched term is greater than the middle term. I know that I am trying to compare two strings and so greater than doesn't apply, but I have no idea how else to do it. Here is my code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    string[] contacts = new string[20];

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        listBox1.Items.Clear(); //Clears the ListBox of all previous items

        if (!File.Exists("Week3List.txt")) //Verifies that the file exists
        {
            MessageBox.Show("You need to write the file first", "Validation", MessageBoxButton.OK); //Notifies the user if the file does not exist
            return;
        }
        using (StreamReader sr = new StreamReader("Week3List.txt")) //Designates the path to the file that was created
        {
            try
            {
                contacts = File.ReadAllLines("Week3List.txt");
                Array.Sort(contacts);
                foreach (string contact in contacts)
                {
                    listBox1.Items.Add(contact);
                }
                sr.Close(); //Closes the StreamReader
            }
            catch (Exception ex) //A catch to handle access errors
            {
                MessageBox.Show(ex.ToString(), "Exception Handler", MessageBoxButton.OK); //Adds the error message to the ListBox
            }
        }
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        bSearch(contacts);
    }

    private void bSearch(string[] contacts)
    {
        int index = Array.BinarySearch(contacts, textBox1.Text);
    }

    public int BinarySearch(string[] contacts, string searchTerm)
    {
        int first = 0;
        int last = contacts.Length - 1;
        int position = -1;
        bool found = false;
        int compCount = 0;
        searchTerm = textBox1.Text;

        while (found != true && first <= last)
        {
            int middle = (first + last) / 2;

            if (contacts[middle] == searchTerm)
            {
                found = true;
                position = middle;
                compCount++;

                MessageBox.Show("Your search has been found after " + compCount + "comparisons.");
            }
            else if (contacts[middle] > searchTerm)
            {
                last = middle;
                compCount++;
            }
            else
            {
                first = middle;
                compCount++;
            }
        }
        return position;
        return compCount;
    }
}
}

Does anyone see where I am going wrong or know of a way to compare the two for a greater or less than value? I thought because it was sorted that it might compare the first letter and determine based off of that but I was wrong.

RegularExpression

Look at this method for comparing strings. It will tell you whether a string is greater, less, or equal (in value) to another string:

http://msdn.microsoft.com/en-us/library/zkcaxw5y%28v=vs.110%29.aspx

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related