Given a List<int> myValues
which I know to be ordered, what is the quickest way to determine if X
is in the given list?
Do I really have to write my own binary search?
Given a List<int> myValues
which I know to be ordered, what is the quickest way to determine if X
is in the given list?
Do I really have to write my own binary search?
I think you can use list.Contains(value)
, but if you really need binary search list already has it implemented: list.BinarySearch()
.
There's a binary search function provided:
List<int> myList = new List<int>() {314,1592,6535};
Console.WriteLine("{0}: {1}", myList.BinarySearch(6535), myList[myList.BinarySearch(6535)]);