C# 计数为BinarySearch进行的比较数

C# 计数为BinarySearch进行的比较数,c#,algorithm,search,count,C#,Algorithm,Search,Count,我的任务是创建两个独立的程序,一个线性搜索程序(我已经完成),和一个二进制搜索程序。这些程序还必须计算搜索过程中进行的比较次数。我的线性搜索程序已经计算了比较的次数,而我的二进制搜索程序却不能。二进制搜索的代码如下所示: using System; using System.Collections.Generic; public class Example { public static void Main() { Console.WriteLine("Input number yo

我的任务是创建两个独立的程序,一个线性搜索程序(我已经完成),和一个二进制搜索程序。这些程序还必须计算搜索过程中进行的比较次数。我的线性搜索程序已经计算了比较的次数,而我的二进制搜索程序却不能。二进制搜索的代码如下所示:

using System;
using System.Collections.Generic;

public class Example
{
public static void Main()
{

    Console.WriteLine("Input number you would like to search for");

    String Look_for = Console.ReadLine();

    int Lookfor;

    int.TryParse(Look_for, out Lookfor);

    {
        List<int> numbers = new List<int>();

        numbers.Add(1); 
        numbers.Add(2); 
        numbers.Add(3); 
        numbers.Add(4); 
        numbers.Add(5); 
        numbers.Add(6); 
        numbers.Add(7); 
        numbers.Add(8); 

        Console.WriteLine();
        foreach (int number in numbers)
        {
            Console.WriteLine(number);
        }

        int answer = numbers.BinarySearch(Lookfor);

        Console.WriteLine("The numbers was found at:");

        Console.WriteLine(answer);

    }
 }
}
使用系统;
使用System.Collections.Generic;
公开课范例
{
公共静态void Main()
{
WriteLine(“输入您要搜索的号码”);
字符串Look_for=Console.ReadLine();
int查找;
int.TRIPARSE(查找,向外查找);
{
列表编号=新列表();
增加(1);
增加(2);
增加(3);
增加(4);
增加(5);
增加(6);
增加(7);
增加(8);
Console.WriteLine();
foreach(整数中的整数)
{
控制台写入线(编号);
}
int answer=numbers.BinarySearch(Lookfor);
Console.WriteLine(“数字位于:”);
控制台。写线(应答);
}
}
}
如果有人能告诉我如何修改它来计算比较,我将不胜感激


非常感谢,马修

您可以编写一个自定义函数来统计它的使用次数,然后在搜索方法中使用它。(或者我想是一个定制的
IEqualityComparer
用于线性搜索。)

这是一种家庭作业吗?
List.BinarySearch
方法不提供此类信息

如果需要比较次数,您必须编写自己的
IComparer
二进制搜索,或者仍然使用.NET方法,根据列表长度和元素位置计算计数。

实现一个计算比较次数的方法:

private class CountComparer : IComparer<int> {

  public int Count { get; private set; }

  public CountComparer() {
    Count = 0;
  }

  public int Compare(int x, int y) {
    Count++;
    return x.CompareTo(y);
  }

}
比较器然后包含计数:

Console.WriteLine("The binary search made {0} comparisons.", comparer.Count);

奖励:任何类似类型的通用计数比较器:

private class CountComparer<T> : IComparer<T> where T : IComparable<T> {

  public int Count { get; private set; }

  public CountComparer() {
    Count = 0;
  }

  public int Compare(T x, T y) {
    Count++;
    return x.CompareTo(y);
  }

}
私有类CountComparer:IComparer其中T:IComparable{
公共整数计数{get;私有集;}
公共计数比较器(){
计数=0;
}
公共整数比较(TX,TY){
计数++;
返回x.CompareTo(y);
}
}

您可以使用此扩展方法(代码基于)

公共静态类ListEx
{
公共静态元组二进制搜索WithCount(
此IList列表(T键)
{
int min=0;
int max=list.Count-1;
int计数器=0;

虽然(min)您有权访问BinarySearch扩展方法吗?您确定要使用内置的方法完成此任务吗?您可以编写一个自定义比较器来计算调用次数,但通常此类练习的重点是自己实现功能……任务是比较两种搜索的效率对于不同大小的列表,如果我使用自己的二进制搜索而不是内置的,那会更容易吗?谢谢你的精彩、详细的回答!我一直在努力寻找答案。对于学习编程的人来说,这个网站是一个非常棒的资源:]谢谢你的奖励,这个网站上的人都很棒。嗨,谢谢你的回答,但我很难把它拼凑成一个功能性的程序。在我最近开始学习C#之前,我只使用了更简单的Real Basic编程。有没有可能把它做成一个功能性的形式,然后我就可以学习了?如果有,我将永远欠你的债,因为这有b甚至一个多星期都让我发疯了:P@MatthewMorgan抱歉,我打赌,我没有学会如何正确地从VS复制粘贴代码!这是固定的:在顶部添加了2行。这将适用于.net 4及更高版本。如果您在另一个版本中需要它,请告诉我。我非常感谢您,我发现了解程序如何使用它非常容易这是一个完整的例子。这帮助我学习了大量关于如何使用类的知识,我对此也有点困惑:]
private class CountComparer<T> : IComparer<T> where T : IComparable<T> {

  public int Count { get; private set; }

  public CountComparer() {
    Count = 0;
  }

  public int Compare(T x, T y) {
    Count++;
    return x.CompareTo(y);
  }

}
public static class ListEx
{
    public static Tuple<int, int> BinarySearchWithCount<T>(
        this IList<T> list, T key)
    {
        int min = 0;
        int max = list.Count - 1;
        int counter = 0;

        while (min <= max)
        {
            counter++;
            int mid = (min + max) / 2;
            int comparison = Comparer<T>.Default.Compare(list[mid], key);
            if (comparison == 0)
            {
                return new Tuple<int, int>(mid, counter);
            }
            if (comparison < 0)
            {
                min = mid + 1;
            }
            else
            {
                max = mid - 1;
            }
        }

        return new Tuple<int, int>(~min, counter);
    }
}

//Which returns a tuple with the item and a number of comparisons. 
//Here is how you can use it:

class Program
{
    static void Main(string[] args)
    {
        var numbers = new List<int>();
        numbers.AddRange(Enumerable.Range(0, 100000));

        var answer = numbers.BinarySearchWithCount(2);
        Console.WriteLine("item: " + answer.Item1);   // item: 2
        Console.WriteLine("count: " + answer.Item2);  // count: 15

    }
}