Javatpoint Logo
Javatpoint Logo

Array.BinarySearch(Array, Int32, Int32, Object) Method with examples in C#

In this article, you will learn about the "Array.BinarySearch(Array, Int32, Int32, Object)" method in C# with its syntax, parameters, and example.

What is Array.BinarySearch Method?

A binary search may be carried out on a one-dimensional, sorted array in C# using the "Array.BinarySearch(Array, Int32, Int32, Object)" method, which is a part of the Array class. This approach can be useful for looking for a certain element inside a given array range. Sorting the array is necessary for the binary Search to work properly. If the array is not sorted, the results are undefined.

The binary Search is an efficient algorithm that frequently divides the search range in half. For large data sets, it is quicker than a linear search with a time complexity of O(logn).

To do the comparison, each element in the array must either provide a custom IComparer implementation or be of a type that implements the IComparable interface.

Syntax:

It has the following syntax:

Parameters:

array: The sorted, one-dimensional array that will be searched.

index: It is the starting index of the range to search.

length: The number of elements in the search range.

value: The element that has to be found within the given range.

Return Value:

  1. If the searched object is found at index 'i', the method returns 'i'.
  2. If the searched object is not found, the method returns a negative number '-(x + 1)', where 'x' is the index of the next element larger than the searched object, or if there are no larger elements, the bitwise complement of the length of the array.
  3. If this method is called with a non-sorted array, the return value can be incorrect, and a negative number could be returned, even if the Value is present in the array.

Exceptions:

  1. ArgumentNullException: If the array is null, raise ArgumentNullException.
  2. RankException: If the array is multidimensional.
  3. ArgumentOutOfRangeException: An ArgumentOutOfRangeException occurs when the range is less than the lower bound or the length is less than zero.
  4. ArgumentException: When a value is of a type incompatible with the array's elements or when the index and length fail to indicate the array's valid range.
  5. InvalidOperationException: An invalid operation exception will occur if an element found during the Search does not implement the IComparable interface and the Value does not.

Example:

Let us take an example to illustrate the Array.BinarySearch() method in C#.

Output:

Full Array Search
Value 11 found at index 5
Subrange Search
Value 11 found at index 5
Not in Array Search
Value 8 not found. The next larger element is at index 4

Explanation:

1. Array Initialization

An array of integers named num_bers is initialized with sorted values.

2. Search Parameters

The target value to search for is 11.

3. Binary Search

Binary searches are executed in two situations using the "Array.BinarySearch" method:

  • The first one searches for target_Value throughout the entire array.
  • The second one searches for target_Value just in the first seven elements.

4. Search for a Value Not in the Array

The method is called to search for a value (notIn_ArrayValue) not present in the array.

5. Result Printing

A helper method, PrintResult, is used to print the results for each case.

It prints whether the Value is found; if not, it provides information about the next larger element.

This example demonstrates scenarios where the target value is searched inside a subrange of the array, not found, and found. After that, it prints results-related informational messages.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA