Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary searching a string array not finding searched string C#

I'm trying to search an object array with text that has been typed into a text box.

I have converted the object array to a string array however, I'm still not having any luck with finding the correct index

I'm using the inbuilt binary search option within c# as that has been requested. I cannot change from this.

If anyone can help that would be great - If you need anything from me don't be afraid to drop me a comment.

Here's the customer array

Customer[] cust = new Customer[20];

Here's the sorting method for the customer class

private void customerSort()
        {
            for (int y = 0; y < 20; y++)
            {
                for (int x = 0; x < customerPTR - 1; x++)
                {
                    if (string.Compare(cust[x].GSname, cust[x + 1].GSname) > 0)
                    {
                        customerSwapRoutine(cust[x]);
                    }
                }
            }
        }

and the swap routine

private void customerSwapRoutine(Customer book, int x = 0)
        {
            string tempString = cust[x].GSname;
            cust[x].GSname = cust[x + 1].GSname;
            cust[x + 1].GSname = tempString;

            string tempString2 = cust[x].GScID;
            cust[x].GScID = cust[x + 1].GScID;
            cust[x + 1].GScID = tempString2;

            string tempString3 = cust[x].GSlocation;
            cust[x].GSlocation = cust[x + 1].GSlocation;
            cust[x + 1].GSlocation = tempString3;

            string tempString4 = cust[x].GSemail;
            cust[x].GSemail = cust[x + 1].GSemail;
            cust[x + 1].GSemail = tempString4;
        }

Here's the customer class

class Customer
    {
        private string name, location, email, cID;

        public string GScID
        {
            get { return cID; }
            set { cID = value; }
        }
        public string GSname
        {
            get { return name; }
            set { name = value; }
        }
        public string GSlocation
        {
            get { return location; }
            set { location = value; }
        }
        public string GSemail
        {
            get { return email; }
            set { email = value; }
        }
        public string displayCustomer()
        {
            return GScID + " " + GSname + " " + GSlocation + " " + GSemail;
        }
    }

Here's the search method

private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            string[] str = new string[cust.Length];

            for(int y = 0; y < cust.Length; y++)
            {
                if(cust[y] == null)
                {
                    Customer nc = new Customer();
                    cust[y] = nc;
                    cust[y].GScID = "";
                    cust[y].GSemail = "";
                    cust[y].GSlocation = "";
                    cust[y].GSname = "";
                }

                str[y] = cust[y].GScID;
            }

            string stringcID = tbCUSTOMERID.Text;

            int found = Array.BinarySearch(str, stringcID);

            if (found < 0)
            {
                MessageBox.Show("Customer doesn't exist");
            }
            else
            {
                MessageBox.Show("Customer found!");
                tbCUSTOMERID.Text = cust[found].GScID;
                tbCUSTOMERNAME.Text = cust[found].GSname;
                tbCITY.Text = cust[found].GSlocation;
                tbEMAIL.Text = cust[found].GSemail;
            }
        }
like image 384
Lerrin Malcoms Avatar asked Oct 15 '22 08:10

Lerrin Malcoms


1 Answers

If you look at this part of your code:

for(int y = 0; y < cust.Length; y++)
{
                if(cust[y] == null)
                {
                    Customer nc = new Customer();
                    cust[y] = nc;
                    cust[y].GScID = "";
                    cust[y].GSemail = "";
                    cust[y].GSlocation = "";
                    cust[y].GSname = "";
                }

                str[y] = cust[y].GScID;
}
string stringcID = tbCUSTOMERID.Text;
int found = Array.BinarySearch(str, stringcID);

You are inserting a lot of new Customer objects into your cust array just before doing a BinarySearch. This will break the existing sorting.

See the documentation

Searches the entire sorted List for an element using the specified comparer and returns the zero-based index of the element.

The whole array should be sorted already just before the BinarySearch. So, either you need to sort your array again after adding these new Customer objects. Or you should add these new Customer objects to the correct index in the sorted strings, so it can keep its correct sorting.

There is another bug, the customerSort function sorts using GSname field. But string[] str array consists of GScID fields. You should sort and search the same things.

So, your code is buggy. If you make sure of sorting, then it should work.

like image 145
Koray Elbek Avatar answered Oct 21 '22 00:10

Koray Elbek