如何使用 C# 二进制搜索找到正确的结果? (打印过程)
How to find correct result using C# binary search? (&print the process)
我想使用 C# 二进制搜索找到正确的结果
这是我的代码
using System;
namespace BinarySearch
{
class Program
{
public static int BinarySearch(int[] Numbers,int SearchItems)
{
int start = 0;
int end = Numbers.Length - 1;
while (start<=end)
{
int mid = (start + end) / 2;
if (SearchItems<Numbers[mid])
{
end = mid - 1;
}
else
{
start = mid + 1;
}
}
return -1;
}
static void Main(string[] args)
{
Console.WriteLine("Please Provide input search No. :");
int SearchItems = Convert.ToInt32(Console.ReadLine());
int[] Numbers = { 8, 44, 26, 67, 35, 12, 77, 75, 31, 2, 62, 4, 88, 17 };
int result = BinarySearch(Numbers, SearchItems);
if (result<0)
{
Console.WriteLine($"{SearchItems} is not found!");
}
else
{
Console.WriteLine($"{SearchItems} is found at index:{result}");
}
}
}
}
当输入为88时,输出不正确
Please Provide input search No. :
88
88 is not found!
为什么会这样?
和
我也想知道怎么把搜索过程打印出来?
感谢您的热心帮助!
尝试:
using System;
namespace BinarySearch
{
class Program
{
public static int BinarySearch(int[] Numbers,int SearchItems)
{
int start = 0;
int end = Numbers.Length - 1;
while (start<=end)
{
int mid = (start + end) / 2;
if (SearchItems == Numbers[mid]) //need this code to check equal value
{
return ++mid;
}
if (SearchItems<Numbers[mid])
{
end = mid - 1;
}
else
{
start = mid + 1;
}
}
return -1;
}
static void Main(string[] args)
{
Console.WriteLine("Please Provide input search No. :");
int SearchItems = Convert.ToInt32(Console.ReadLine());
int[] Numbers = { 8, 44, 26, 67, 35, 12, 77, 75, 31, 2, 62, 4, 88, 17 };
Array.Sort(Numbers);
int result = BinarySearch(Numbers, SearchItems);
if (result<0)
{
Console.WriteLine($"{SearchItems} is not found!");
}
else
{
Console.WriteLine($"{SearchItems} is found at index:{result}");
}
}
}
}
我想使用 C# 二进制搜索找到正确的结果
这是我的代码
using System;
namespace BinarySearch
{
class Program
{
public static int BinarySearch(int[] Numbers,int SearchItems)
{
int start = 0;
int end = Numbers.Length - 1;
while (start<=end)
{
int mid = (start + end) / 2;
if (SearchItems<Numbers[mid])
{
end = mid - 1;
}
else
{
start = mid + 1;
}
}
return -1;
}
static void Main(string[] args)
{
Console.WriteLine("Please Provide input search No. :");
int SearchItems = Convert.ToInt32(Console.ReadLine());
int[] Numbers = { 8, 44, 26, 67, 35, 12, 77, 75, 31, 2, 62, 4, 88, 17 };
int result = BinarySearch(Numbers, SearchItems);
if (result<0)
{
Console.WriteLine($"{SearchItems} is not found!");
}
else
{
Console.WriteLine($"{SearchItems} is found at index:{result}");
}
}
}
}
当输入为88时,输出不正确
Please Provide input search No. :
88
88 is not found!
为什么会这样? 和 我也想知道怎么把搜索过程打印出来?
感谢您的热心帮助!
尝试:
using System;
namespace BinarySearch
{
class Program
{
public static int BinarySearch(int[] Numbers,int SearchItems)
{
int start = 0;
int end = Numbers.Length - 1;
while (start<=end)
{
int mid = (start + end) / 2;
if (SearchItems == Numbers[mid]) //need this code to check equal value
{
return ++mid;
}
if (SearchItems<Numbers[mid])
{
end = mid - 1;
}
else
{
start = mid + 1;
}
}
return -1;
}
static void Main(string[] args)
{
Console.WriteLine("Please Provide input search No. :");
int SearchItems = Convert.ToInt32(Console.ReadLine());
int[] Numbers = { 8, 44, 26, 67, 35, 12, 77, 75, 31, 2, 62, 4, 88, 17 };
Array.Sort(Numbers);
int result = BinarySearch(Numbers, SearchItems);
if (result<0)
{
Console.WriteLine($"{SearchItems} is not found!");
}
else
{
Console.WriteLine($"{SearchItems} is found at index:{result}");
}
}
}
}