Generic Internal Binary Search - CSharp Data Structure Algorithm

CSharp examples for Data Structure Algorithm:Sort

Description

Generic Internal Binary Search

Demo Code


using System.Security;
using System.Runtime.Versioning;
using System.Runtime.CompilerServices;

public class Main{
    internal static int InternalBinarySearch(T[] array, int index, int length, T value, IComparer<T> comparer)
    {/*from  w  ww  .  j  a  v a  2  s  .c o m*/
      int num1 = index;
      int num2 = index + length - 1;
      while (num1 <= num2)
      {
        int index1 = num1 + (num2 - num1 >> 1);
        int num3 = comparer.Compare(array[index1], value);
        if (num3 == 0)
          return index1;
        if (num3 < 0)
          num1 = index1 + 1;
        else
          num2 = index1 - 1;
      }
      return ~num1;
    }
}

Related Tutorials