二分查询

static void Main(string[] args)
{
int[] array = { 0, 54, 6, 79, 15, 35, 45 };
Array.Sort(array);
Console.WriteLine("数字排序后:");
foreach (var i in array)
{
Console.Write(i + ",");
}
Console.WriteLine();
int a = SearchFun(array, 79);
Console.WriteLine("找到的值:" + a);

Console.Read();

}

static int SearchFun(int[] array,int value)

{

int low,mid,high;

low=0;

high=Array.length-1;

while(low<high)

{

mid=(low+high)/2;

if(array[mid]==value)

return array[mid];

if(array[mid]>value)

high=mid-1;

else

low=mid+1;

return-1;

}

}

posted @ 2018-10-31 19:33  0ooooooooooooooooo0  阅读(134)  评论(0编辑  收藏  举报