As part of small programme I need to find the nearest value in the X data of a (AFU) record to the value specifed by the user. I am using .BinarySearch for that but I'd like some advise/pointer.
1. Is this the best way of searching for the nearest value?
2. (assuming tha the exact value is not in the list) is there a way or returnign the "true" nearest value? I lifted the example from microsoft website, see below for part of my test code, and it will always return the rank above. if you search for say 252 in the following list (200,250,300) then you'll get 300 whereas the nearest is 250. Is the only to compare values at (index-1) & (index) ?
3. Maybe I am better of using a Dictionary()
index = ListToSearch.BinarySearch(dbValueToFind) theLW.WriteLine("index is " & index.ToString) If index < 0 Then index = index Xor -1 theLW.WriteLine(" value at the index is " & ListToSearch.Item(index).ToString) Else theLW.WriteLine("value at the index is " & ListToSearch.Item(index).ToString) End if
Perhaps a strategy similar to this:
I don't think a dictionary is the answer to this particular problem.
Thanks. I cannot use LINQ
I thought that there would be something easy to use! Looks like I may need a simple check (calculate the difference between the value to find and the value at index, index-1