Implements a binary search similar to , except that the comparer can be used to compare array elements with some arbitrary search value. - CSharp System

CSharp examples for System:Array Search

Description

Implements a binary search similar to , except that the comparer can be used to compare array elements with some arbitrary search value.

Demo Code

// Copyright 2014 The Chromium Authors. All rights reserved.
using System.Collections.Generic;
using System;/*from   w  w  w .j av  a 2  s.c o m*/

public class Main{
        /// <summary>
    /// Implements a binary search similar to <see
    /// cref="Array.BinarySearch(Array, int, int, object, System.Collections.IComparer)"/>,
    /// except that the comparer can be used to compare array elements with some arbitrary search value.
    /// </summary>
    public static int BinarySearch<TElement, TValue>(IList<TElement> array, int index, int length, TValue value, Func<TElement, TValue, int> valueComparer) {
      var max = index + length - 1;
      var cur = 0;
      while (cur <= max) {
        var median = GetMedian(cur, max);

        var compareResult = valueComparer(array[median], value);
        if (compareResult < 0) {
          cur = median + 1;
        } else if (compareResult > 0) {
          max = median - 1;
        } else {
          return median;
        }
      }
      return ~cur;
    }
        /// <summary>
    /// Implements a binary search similar to <see
    /// cref="Array.BinarySearch(Array, object, System.Collections.IComparer)"/>,
    /// except that the comparer can be used to compare array elements with some arbitrary search value.
    /// </summary>
    public static int BinarySearch<T, TKey>(IList<T> array, TKey item, Func<T, TKey, int> itemComparer) {
      return BinarySearch(array, 0, array.Count, item, itemComparer);
    }
}

Related Tutorials