C# 我应该如何进行二进制搜索算法

C# 我应该如何进行二进制搜索算法,c#,C#,对于我们的类的acedemic练习,我们需要制作一个可以执行二进制搜索的程序,然后显示目标的索引。这是我现在的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Binarysearch { class Program { static void Main

对于我们的类的acedemic练习,我们需要制作一个可以执行二进制搜索的程序,然后显示目标的索引。这是我现在的代码:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Binarysearch
{
    class Program
    {
        static void Main(string[] args)
        {
            var target = 10; //Defines what the program will be searching for
            int[] array = { 2, 4, 6, 8, 10, 12, 15, 17, 19, 21, 99999 };
            var last = array.Last();
            var first = array.First();
            var mid = array[array.Length / 2];
            bool found = false;

            while (first <= last && found == false)
            {
                if (mid > target)
                {

                }
                if (mid == target)
                {
                    bool found = true;
                }
                if (mid < target)
                {

                }
            }
            if (first > last){ //If the target was not found
                Console.WriteLine("The target " + target + "was not located.");
                Console.ReadLine();
            }
            if (found == true) //If the target was found
            {
                Console.WriteLine("The target " + target + " was located at index " + mid); // Prints the final outcome.
                Console.ReadLine();
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间二进制搜索
{
班级计划
{
静态void Main(字符串[]参数)
{
var target=10;//定义程序将搜索的内容
int[]数组={2,4,6,8,10,12,15,17,19,21,99999};
var last=array.last();
var first=array.first();
var mid=数组[array.Length/2];
bool-found=false;
while(第一个目标)
{
}
如果(mid==目标)
{
布尔发现=真;
}
中频(中频<目标)
{
}
}
if(first>last){//如果找不到目标
WriteLine(“未找到目标“+目标+”);
Console.ReadLine();
}
if(found==true)//如果找到了目标
{
WriteLine(“目标“+target+”位于索引“+mid);//打印最终结果。
Console.ReadLine();
}
}
}
}
所以我遇到的问题是: 我应该删除数组的上/下半部分,找到中间部分并重复吗?如果我这样做,它最终会找到目标,但我认为它无法找到索引,因为数组的其余部分将被破坏

我该怎么做


谢谢:p

二进制搜索负责在排序数组中查找目标的索引。它不应该以任何方式改变原始数组

下面是一个用C#编写的二进制搜索示例:


搜索从不删除任何内容。这是一种搜索,而不是搜索或销毁操作。在web上,甚至在堆栈溢出上,二进制搜索算法的例子并不缺乏。如果你正在寻找如何做的建议,或者通常是如何做的,那将是第一个开始。我当然同意前面的评论。公共静态int-BinarySearch(int[]intar,int-target)这一行可能重复什么意思?这是方法的减速。
public static int BinarySearch(int[] intArr, int target)
{
    int min = 1, max = intArr.Length;
    int mid;

    while (min <= max)
    {
        mid = min + (max - min) / 2;
        if (intArr[mid] == target)
            return mid;

        else if (intArr[mid] < target)
            min = mid + 1;
        else
            max = mid - 1          
    }
    return -1; // Target was not found within the array.
}
int[] intArr = new int[] { 1, 5, 7, 9, 12, 15, 16, 17, 27, 28, 222, 235, 567, 578, 890 };
MessageBox.Show(BinarySearch(intArr, 99).ToString());