Array - BinarySearch

Searches an entire one-dimensional sorted for a specific element, using the interface implemented by each element of the and by the specified object.

Try it

public static void Main()
{
	Array myIntArray = Array.CreateInstance(typeof(Int32), 5);

    myIntArray.SetValue(8, 0);
    myIntArray.SetValue(2, 1);
    myIntArray.SetValue(6, 2);
    myIntArray.SetValue(3, 3);
    myIntArray.SetValue(7, 4);

    // Do the required sort first
    Array.Sort(myIntArray);

    // Displays the values of the Array.
    Console.WriteLine( "The Int32 array contains the following:" );
    PrintValues(myIntArray);
	
	object myObject = 5;
	// C# Extension Method: Array - BinarySearch
	int myIndex=Array.BinarySearch(myIntArray, myObject);
	
    if (myIndex < 0)
    {
        Console.WriteLine("The object to search for ({0}) is not found. The next larger object is at index {1}.", myObject, ~myIndex );
    }
    else
    {
        Console.WriteLine("The object to search for ({0}) is at index {1}.", myObject, myIndex );
    }
}

public static void PrintValues(Array myArr)
{
    int i = 0;
    int cols = myArr.GetLength(myArr.Rank - 1);
    foreach (object o in myArr)
    {
        if ( i < cols )
        {
            i++;
        }
        else
        {
            Console.WriteLine();
            i = 1;
        }
        Console.Write( "\t{0}", o);
    }
    Console.WriteLine();
}

View Source
using System;
using System.Collections;

public static partial class Extensions
{
    /// <summary>
    ///     Searches an entire one-dimensional sorted  for a specific element, using the  interface implemented by each
    ///     element of the  and by the specified object.
    /// </summary>
    /// <param name="array">The sorted one-dimensional  to search.</param>
    /// <param name="value">The object to search for.</param>
    /// <returns>
    ///     The index of the specified  in the specified , if  is found. If  is not found and  is less than one or more
    ///     elements in , a negative number which is the bitwise complement of the index of the first element that is
    ///     larger than . If  is not found and  is greater than any of the elements in , a negative number which is the
    ///     bitwise complement of (the index of the last element plus 1).
    /// </returns>
    public static Int32 BinarySearch(this Array array, Object value)
    {
        return Array.BinarySearch(array, value);
    }

    /// <summary>
    ///     Searches a range of elements in a one-dimensional sorted  for a value, using the  interface implemented by
    ///     each element of the  and by the specified value.
    /// </summary>
    /// <param name="array">The sorted one-dimensional  to search.</param>
    /// <param name="index">The starting index of the range to search.</param>
    /// <param name="length">The length of the range to search.</param>
    /// <param name="value">The object to search for.</param>
    /// <returns>
    ///     The index of the specified  in the specified , if  is found. If  is not found and  is less than one or more
    ///     elements in , a negative number which is the bitwise complement of the index of the first element that is
    ///     larger than . If  is not found and  is greater than any of the elements in , a negative number which is the
    ///     bitwise complement of (the index of the last element plus 1).
    /// </returns>
    public static Int32 BinarySearch(this Array array, Int32 index, Int32 length, Object value)
    {
        return Array.BinarySearch(array, index, length, value);
    }

    /// <summary>
    ///     Searches an entire one-dimensional sorted  for a value using the specified  interface.
    /// </summary>
    /// <param name="array">The sorted one-dimensional  to search.</param>
    /// <param name="value">The object to search for.</param>
    /// <param name="comparer">
    ///     The  implementation to use when comparing elements.-or- null to use the  implementation
    ///     of each element.
    /// </param>
    /// <returns>
    ///     The index of the specified  in the specified , if  is found. If  is not found and  is less than one or more
    ///     elements in , a negative number which is the bitwise complement of the index of the first element that is
    ///     larger than . If  is not found and  is greater than any of the elements in , a negative number which is the
    ///     bitwise complement of (the index of the last element plus 1).
    /// </returns>
    public static Int32 BinarySearch(this Array array, Object value, IComparer comparer)
    {
        return Array.BinarySearch(array, value, comparer);
    }

    /// <summary>
    ///     Searches a range of elements in a one-dimensional sorted  for a value, using the specified  interface.
    /// </summary>
    /// <param name="array">The sorted one-dimensional  to search.</param>
    /// <param name="index">The starting index of the range to search.</param>
    /// <param name="length">The length of the range to search.</param>
    /// <param name="value">The object to search for.</param>
    /// <param name="comparer">
    ///     The  implementation to use when comparing elements.-or- null to use the  implementation
    ///     of each element.
    /// </param>
    /// <returns>
    ///     The index of the specified  in the specified , if  is found. If  is not found and  is less than one or more
    ///     elements in , a negative number which is the bitwise complement of the index of the first element that is
    ///     larger than . If  is not found and  is greater than any of the elements in , a negative number which is the
    ///     bitwise complement of (the index of the last element plus 1).
    /// </returns>
    public static Int32 BinarySearch(this Array array, Int32 index, Int32 length, Object value, IComparer comparer)
    {
        return Array.BinarySearch(array, index, length, value, comparer);
    }
}