二分查找法数据应该是有序的吧
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication4
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
static int h = 0;
static int[] arr ={2,3,6,8,10,45,46, 434 };
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{ half(8, 0, 7);
half(46, 0, 7);
} private static void half(int key,int start,int end)
{
if (start > end)
{
Console.Write("not found");
Console.Read();
}
h = (start + end) / 2;
if (arr[h] == key)
{
Console.Write("the pos =" + h.ToString());
Console.Read(); }
else
{
if (arr[h] > key)
{
half(key, start, h - 1);
}
else
{
half(key, h + 1, end);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication4
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
static int h = 0;
static int[] arr ={2,3,6,8,10,45,46, 434 };
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{ half(8, 0, 7);
half(46, 0, 7);
} private static void half(int key,int start,int end)
{
if (start > end)
{
Console.Write("not found");
Console.Read();
}
h = (start + end) / 2;
if (arr[h] == key)
{
Console.Write("the pos =" + h.ToString());
Console.Read(); }
else
{
if (arr[h] > key)
{
half(key, start, h - 1);
}
else
{
half(key, h + 1, end);
}
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货