I have an object array and I want to search the date / day property (user decides) by using binary search.
**EDIT: I read the SharesArray[mid].Date values (and day values) from a text file and examples are: 05/02/2015, 14/10/2014.The searchString value would be gained from the user but would be in the same format as the date values. **
This is my first attempt so I am just trying the date property:
int high, low, mid;
high = SharesArray.Length - 1;
low = 0;
while (low <= high)
{
mid = (low + high) / 2;
if (String.Compare(SharesArray[mid].Date, searchString, true) == 0)
{
break;
}
else if (String.Compare(SharesArray[mid].Date, searchString, true) > 0)
{
high = mid - 1;
}
else if (String.Compare(SharesArray[mid].Date, searchString, true) < 0)
{
low = mid + 1;
}
I have also tried the last else if statement as just else and that doesn't make any difference.
Also, does it matter what way round string1 and string2 are in the String.Compare part?
Dateis defined inSharesArrayand give us an example of what valuesearchStringcould be and what possible valuesSharesArray[mid].Datecould be?Comparecall is most definitely not irrelevant and themidcalculation is correct, although it is potentially vulnerable to an overflow, somid = low + ((high - low) >> 1)would be better