C#二进制搜索不返回结果

C#二进制搜索不返回结果,c#,binary-search,C#,Binary Search,代码: 使用系统; 使用System.IO; 使用System.Collections.Generic; 类主类{ 公共静态void Main(字符串[]args){ Console.WriteLine(“获取随机名称”); //读取文件中的每一行。 列表名称列表=新列表(); 使用(StreamReader=newstreamreader(“test.txt”)) { 弦线; 而((line=reader.ReadLine())!=null) { 名称列表。添加(行); } } nameLis

代码:

使用系统;
使用System.IO;
使用System.Collections.Generic;
类主类{
公共静态void Main(字符串[]args){
Console.WriteLine(“获取随机名称”);
//读取文件中的每一行。
列表名称列表=新列表();
使用(StreamReader=newstreamreader(“test.txt”))
{
弦线;
而((line=reader.ReadLine())!=null)
{
名称列表。添加(行);
}
}
nameList.Sort();
int startValue=0;
int middleValue=(nameList.Count+1)/2;
int endValue=(nameList.Count+1);
WriteLine(“输入要搜索的名称”);
字符串名称=Console.ReadLine();
bool nameNotFound=true;
var compareResult=String.Compare(名称列表[middleValue],名称);
while(nameNotFound){
如果(比较结果<0){
endValue=中间值;
中间值=端值/2;
}
否则如果(比较结果>0){
起始值=中间值;
中间值=(结束值-起始值)/2+起始值;
}
否则{
WriteLine(“找到名称”+名称+”);
nameNotFound=false;
}
}
}
}
问题: 我正在尝试编写一个C#二进制搜索代码,用于搜索具有列表名称(字符串)的文件。由于某种原因,我不知道,搜索不会返回任何结果。有人有什么想法吗

解决方案: 我现在已经修好了密码。这两个问题是我没有比较if和else if循环中的值,我把大于和小于符号混淆了

using System;
using System.IO;
using System.Collections.Generic;

class MainClass {
    public static void Main (string[] args) {
        Console.WriteLine ("Get Random Names");
        // Read every line in the file.
        List<string> nameList = new List<string>();
        using (StreamReader reader = new StreamReader("test.txt"))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
        {
            nameList.Add(line);
        }
    }

        nameList.Sort();
        int startValue = 0;
        int middleValue = (nameList.Count + 1) / 2;
        int endValue = (nameList.Count + 1);

        Console.WriteLine("Enter a name to search for.");
        String name = Console.ReadLine();

        bool nameNotFound = true;
        var compareResult = String.Compare(nameList[middleValue], name);


        while (nameNotFound) { 
            if (compareResult < 0) {
              endValue = middleValue;
              middleValue = endValue / 2;
            }
            else if (compareResult > 0) {
              startValue = middleValue;
              middleValue = (endValue - startValue) / 2 + startValue;
            }
            else {
              Console.WriteLine("Name " + name + " was found.");
              nameNotFound = false;
            }
        }
}
}

while(nameNotFound){//一个不再比较的循环
//(让我想知道你到底是怎么退出这个项目的)
如果(比较结果<0){
endValue=中间值;
中间值=端值/2;
}
否则如果(比较结果>0){
起始值=中间值;
中间值=(结束值-起始值)/2+起始值;
}
否则{
WriteLine(“找到名称”+名称+”);
nameNotFound=false;
}
}
TL;DR是指您提供的代码只检查文本文档中最中间的字符串是否与提供的名称相同

首先,你需要(再次)进行比较

while(nameNotFound){//一个不再比较的循环
//(让我想知道你到底是怎么退出这个项目的)
如果(比较结果<0){
endValue=中间值;
中间值=端值/2;
}
否则如果(比较结果>0){
起始值=中间值;
中间值=(结束值-起始值)/2+起始值;
}
否则{
WriteLine(“找到名称”+名称+”);
nameNotFound=false;
}
}
TL;DR是指您提供的代码只检查文本文档中最中间的字符串是否与提供的名称相同


您需要(再次)比较启动器

在最后的主算法循环中,您永远不会重新计算
比较结果
,因此您的程序无法判断何时找到了什么

您需要在
if
else if
块中添加
compareResult=String.compare…


如果不这样做,
compareResult
将保留在循环之前进行的第一次比较的结果。

在最后的主算法循环中,您永远不会重新计算
compareResult
,因此您的程序无法判断何时找到了某些内容

您需要在
if
else if
块中添加
compareResult=String.compare…


如果不这样做,
compareResult
将保留循环前第一次比较的结果。

尝试理解旧答案,然后尝试重写增强代码,如果失败,请不要沮丧,请查看此处:

    while (nameNotFound) { // A loop that never compares again
                           // (and makes me wonder how you actually exit the program)
        if (compareResult < 0) {
          endValue = middleValue;
          middleValue = endValue / 2;
        }
        else if (compareResult > 0) {
          startValue = middleValue;
          middleValue = (endValue - startValue) / 2 + startValue;
        }
        else {
          Console.WriteLine("Name " + name + " was found.");
          nameNotFound = false;
        }
    }
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用System.IO;
命名空间StackOverflowSolver
{
班级计划
{
公共静态void Main(字符串[]args)
{
Console.WriteLine(“获取随机名称”);
//读取文件中的每一行。
列表名称列表=新列表();
使用(StreamReader=newstreamreader(“test.txt”))
{
弦线;
而((line=reader.ReadLine())!=null)
{
名称列表。添加(行);
}
}
nameList.Sort();
//用索引替换值可以得到更好的变量名
int startValue=0;
//int middleValue=(nameList.Count+1)/2;错误
//假设你有三个元素,中间值是2
//数组索引从0开始记住
int middleValue=nameList.Count/2;
//int endValue=(nameList.Count+1);错误
int endValue=nameList.Count-1;
WriteLine(“输入要搜索的名称”);
字符串名称=Console.ReadLine();
bool nameNotFound=true;
while((nameNotFound)&&(endValue>startValue))//添加结束搜索条件
{
var compareResult=String.Compare(名称、名称列表[middleValue]);
如果(比较结果<0)
{
endValue=middleValue-1;//减1
中间值=端值/2;
}
否则如果(比较结果>0)
{
startValue=middleValue+1;//添加1
中间值=(结束值-起始值)/2+星
    while (nameNotFound) { // A loop that never compares again
                           // (and makes me wonder how you actually exit the program)
        if (compareResult < 0) {
          endValue = middleValue;
          middleValue = endValue / 2;
        }
        else if (compareResult > 0) {
          startValue = middleValue;
          middleValue = (endValue - startValue) / 2 + startValue;
        }
        else {
          Console.WriteLine("Name " + name + " was found.");
          nameNotFound = false;
        }
    }
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace StackOverflowSolver
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Get Random Names");
            // Read every line in the file.
            List<string> nameList = new List<string>();
            using (StreamReader reader = new StreamReader("test.txt"))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    nameList.Add(line);
                }
            }

            nameList.Sort();

            //you can get better variables name with replacing Value with Index
            int startValue = 0;
            //int middleValue = (nameList.Count + 1) / 2;   error
            //consider you've three elements, middleValue will be 2 
            //the array index began from 0 remeber that
            int middleValue = nameList.Count / 2;
            //int endValue = (nameList.Count + 1);          error 
            int endValue = nameList.Count-1;
            Console.WriteLine("Enter a name to search for.");
            String name = Console.ReadLine();

            bool nameNotFound = true;
            while ((nameNotFound) && (endValue > startValue))//add end search condition
            {
                var compareResult = String.Compare(name, nameList[middleValue]);
                if (compareResult < 0)
                {
                    endValue = middleValue -1; //Substract 1
                    middleValue = endValue / 2;
                }
                else if (compareResult > 0)
                {
                    startValue = middleValue +1; //Add 1
                    middleValue = (endValue - startValue) / 2 + startValue;
                }
                else
                {
                    Console.WriteLine("Name " + name + " was found.");
                    nameNotFound = false;
                }
            }
            //consider to uncommit the line below
            //Console.WriteLine("Name " + name + " not found!"); //inform not found
            Console.ReadKey();
        }
    }
}