C# ArrayList - BinarySearch() Method



The C# ArrayList BinarySearch() method is used to search a specific element efficiently within a sorted ArrayList.

This method performs a binary search, which is much faster than a linear search, but it requires the ArrayList Sorted.

Syntax

Following are the syntax of the C# ArrayList BinarySearch() method −

public virtual int BinarySearch(object value);

public virtual int BinarySearch(object value, IComparer comparer);

public virtual int BinarySearch(int index, int count, object value, IComparer comparer);

Parameters

This method accepts the following parameters −

  • value: It is required to specify the object to search for in the ArrayList. It can be null for reference types.
  • comparer: It is optional, which Icomparer implementation to customize the comparison.
  • index: It is a zero-based starting index of the range to search.
  • count: It represents the number of elements in the range to search.

Return value

This method returns a zero-based index of the value in the sorted ArrayList if the value is found. Otherwise, a negative number.

Example 1: Add Elements in Range

This is the basic example of the ArrayList collection to demonstrate how to use BinarySearch to search a specific object in the ArrayList −

    
using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { 10, 20, 30, 40, 50 };

      // Perform BinarySearch
      int index = arrayList.BinarySearch(30);

      if (index >= 0)
      {
         Console.WriteLine($"Element found at index: {index}");
      }
      else
      {
         Console.WriteLine("Element not found");
      }
   }
}

Output

Following is the output −

Element found at index: 2

Example 2: What if The ArrayList is Not Sorted

Let us see another example of the BinarySearch() method. If the ArrayList is not sorted, we first need to sort it before performing the search operation −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { 10, 30, 20, 60, 40, 15 };
      arrayList.Sort();

      // Perform BinarySearch
      int index = arrayList.BinarySearch(30);

      if (index >= 0)
      {
         Console.WriteLine($"Element found at index: {index}");
      }
      else
      {
         Console.WriteLine("Element not found");
      }
   }
}

Output

Following is the output −

Element found at index: 3

Example 3: Using a Custom Comparer

In this example, we use the BinarySearch method with custom comparer to find the specific elements from the ArrayList −

using System;
using System.Collections;

class DescendingComparer: IComparer {
   public int Compare(object x, object y) {
      // reverse order comparsion
      return Comparer.Default.Compare(y, x);
   }
}

class Program {
   static void Main() {
      // Create and sort the ArrayList in descending order
      ArrayList arrayList = new ArrayList {
         50,
         40,
         30,
         20,
         10
      };
      arrayList.Sort(new DescendingComparer());

      // Perform BinarySearch using the custom comparer
      int index = arrayList.BinarySearch(30, new DescendingComparer());

      if (index >= 0) {
         Console.WriteLine($"Element found at index: {index}");
      } else {
         Console.WriteLine("Element not found");
      }
   }
}

Output

Following is the output −

Element found at index: 2

Example 4: Searching a Range of Elements

In this example, we use the BinarySearch() method to search an element with the range −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      // Create and sort the ArrayList
      ArrayList arrayList = new ArrayList { 10, 20, 30, 40, 50 };
      arrayList.Sort();

      // Perform BinarySearch in a specific range
      int index = arrayList.BinarySearch(1, 3, 40, null);

      if (index >= 0)
      {
         Console.WriteLine($"Element found at index: {index}");
      }
      else
      {
         Console.WriteLine("Element not found in the range");
      }
   }
}

Output

Following is the output −

Element found at index: 3
csharp_arraylist.htm
Advertisements