I'm getting the error of not being able to apply indexing, but I'm not sure how establish a 'bounds'(algebraically) for the BinarySearch to find a string. It's apparent that
if (item > N[mid])
can't function because item is a string. How do I find item in this?:
public static int BinarySearch(string[] name, string item)
{
int min = 0;
int N = name.Length;
int max = N - 1;
do
{
int mid = (min + max) / 2;
if (item > N[mid])
min = mid + 1;
else
max = mid - 1;
if (name[mid] == item)
return mid;
//if (min > max)
// break;
} while (min <= max);
return -1;
}
and trying to appease it with something like this
public static int BinarySearch(string[] name, string searchKeyword)
{
int min = 0; //=0
int N = name.Length; //.Length
int max = N - 1;
int S = searchKeyword.Length;
do
{
int mid = (min + max) / 2;
if (S > N[mid])
min = mid + 1;
else
max = mid - 1;
if (name[mid] == S)
return mid;
//if (min > max)
// break;
} while (min <= max);
return -1;
}
You cannot use the
>
operator in a string because C# does not know what do you mean when saying that a string is bigger than another. Do you compare by length, alphabetical order...?.If you want to sort them alphabetically, use the String.Compare method instead: