List<T>.BinarySearch() মেথড সর্টেড লিস্টে একটি এলিমেন্টের ইনডেক্স খুঁজে বের করে (O(log n))। লিস্টটি অবশ্যই সর্টেড থাকতে হবে।
উদাহরণ
using System;
using System.Collections.Generic;
class Demo
{
static void Main()
{
List list = new List();
list.Add(70);
list.Add(150);
list.Add(220);
list.Add(250);
list.Add(300);
int index = list.BinarySearch(250);
Console.WriteLine("Element 250 at Index: " + index);
}
}
আউটপুট
Element 250 at Index: 3
যদি এলিমেন্ট না থাকে, নেগেটিভ মান ফেরত দেয় (বিটওয়াইজ কমপ্লিমেন্ট অফ নেক্সট লার্জার ইনডেক্স)। Array.BinarySearchও রয়েছে।