using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
protected void select(int read)
{
int pre, last, curr;
int[] arr = new int[]{1,2,3,4,5,6} ;
curr = 0;
last= arr.Length;
pre = 0;
while(pre<=last)
{
curr = (pre + last) / 2;
if (arr[curr] == read)
{
Console.WriteLine("结果位置在数组中第:"+(curr+1)+"位");
return;
}
else
{
if (read >arr[curr])
{
pre = curr+1;
}
else
{
last = curr - 1;
}
}
}
}
static void Main(string[] args)
{
Program p = new Program();
while(true)
{
Console.Write("Please enter an integer:");
int get = Convert.ToInt32(Console.ReadLine());
p.select(get);
Console.ReadLine();
}
}
}
}
对半查找算法
最新推荐文章于 2022-04-04 16:32:47 发布