This method searches for an element which is in a one-dimensional sorted array within a range of elements using a specified IComparer interface.
public static int BinarySearch(Array arr, int index, int length, Object value, IComparer comparer)
- arr : The sorted one-dimensional Array which is to be searched.
- index : The starting index of the range from which searching will start.
- length : The length of the range in which the search will happen.
- value : The object to search for.
- comparer : When comparing elements then use the IComparer implementation.
Return Value: It returns the index of the specified value in the specified array if the value is found otherwise it returns a negative number. There are different cases of return values as follows:
- If the value is not found and value is less than one or more elements in the array, the negative number returned is the bitwise complement of the index of the first element that is larger than value.
- If the value is not found and value is greater than all elements in the array, the negative number returned is the bitwise complement of (the index of the last element plus 1).
- 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.
Below programs illustrate the use of the above-discussed method:
Example 1: In this example the array stores some string value and find some string value after sorting the array.
The original Array ABCD IJKL XYZ EFGH MNOP sorted array ABCD EFGH IJKL MNOP XYZ 1st call The object EFGH is at index 1 2nd call The object ABCD is at index 0
Example 2: In this example, here we use “CreateInstance()” method to create a typed array and stores some integer value and search some values after sort the array.
The original Array 20 10 30 40 50 80 70 60 sorted array 10 20 30 40 50 60 70 80 1st call The object 10 is not found Next larger object is at index 1 2nd call The object 60 is not found Next larger object is at index 5
- C# | Buffer.BlockCopy(Array, Int32, Array, Int32, Int32) Method
- Array.BinarySearch(Array, Int32, Int32, Object) Method with examples in C#
- C# | Array.BinarySearch(Array, Object, IComparer) Method
- C# | Char.ConvertToUtf32(String, Int32) Method
- C# | Char.ConvertFromUtf32(Int32) Method
- C# | Char.IsControl(String, Int32) Method
- C# | Char.GetUnicodeCategory(String, Int32) Method with Examples
- C# | Char.IsHighSurrogate(String, Int32) Method
- C# | Char.IsSurrogate(String, Int32) Method
- C# | Char.IsLowSurrogate(String, Int32) Method
- C# | Char.IsSurrogatePair(String, Int32) Method
- Int32.GetTypeCode Method in C# with Examples
- Int32.GetHashCode Method in C# with Examples
- Int32.CompareTo Method in C# with Examples
- Int32.Equals Method in C# with Examples
- C# | Int32.ToString Method | Set - 1
- C# | Int32.ToString Method | Set - 2
- File.Create(String, Int32, FileOptions, FileSecurity) Method in C# with Examples
- Int32.Parse(String) Method in C# with Examples
- File.Create(String, Int32) Method in C# with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.