Been asked to create a binary search method for a 2D array that sorts through strings. I'm doing as part of a course that has lots of specifics, i can't use any pre-built search methods but i have to create this without any learning content, all I've been provided is a standard Binary search method for a 1D array containing Integers, so not much help.
private void BtnSearch_Click(object sender, EventArgs e)
{
int startIndex = 0;
int finalIndex = gameArray.Length -1;
bool flag2 = false;
string searchTerm = txtSearch.Text;
int foundIndex = -1;
while (!flag2 && !((finalIndex - startIndex) <= 1))
{
int middle = (finalIndex + startIndex) / 2;
if (string.Compare(gameArray[middle, 0], searchTerm, true) == 0)
{
foundIndex = middle;
flag2 = true;
break;
}
else
{
if (string.Compare(gameArray[middle, 0], searchTerm, true) > 0)
finalIndex = middle;
else
startIndex = middle;
}
}
if (flag2)
lstGames.SelectedIndex = foundIndex;
else
MessageBox.Show("Not Found");
}
I keep getting this error message any time i execute the search in the program.
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
Which happens on the While loops execution
Really not too sure what I'm doing incorrect at this point.
You should check if "middle" is equal or greater than length of the array before test.
Avoid use !((finalIndex-startIndex)<=1) when you have (finalIndex-startIndex> 1). So much clean.
!flag2 is unnecessary in while clause because only breaks after flag2 is true;
Edit: you aren't solving the index offset problems. Take a look at this BS basic loop
int minIndex=0;
int maxIndex=data.Length-1;
while (minIndex <=maxIndex) { //this is the only control you have to do
int mid = (minIndex + maxIndex) / 2;
if (key == data[mid]) { //compare, or do stuff
return ++mid;
} else if (key < data[mid]) { //compare or do stuff
maxIndex = mid - 1; //this -1 is important in performance
}else {
minIndex = mid + 1; //this +1 is important in performance
}
}
And i recommend you follow SRP (Single responsability bla bla). Make "BinarySearch" a standalone method, and make it return found index or -1 if not found, and use that method where you want. You can make it even generic.