二分法查找有序数
2010-10-29 17:59 通心菜 阅读(273) 评论(0) 编辑 收藏 举报
public void get(int i)
{
string[] str = { "1", "2", "3", "4", "6", "9", "102" };
int front, end, mid;
front = 0;
end = str.Length - 1;
mid = (front + end) / 2;
int k = 0;
while (front < end && int.Parse(str[mid]) != i)
{
k++;
if (int.Parse(str[mid]) < i) front = mid + 1;
if (int.Parse(str[mid]) > i) end = mid - 1;
mid = (front + end) / 2;
}
if (int.Parse(str[mid]) != i)
{
Console.WriteLine("wu");
Console.ReadLine();
}
else
{
Console.WriteLine("第"+k.ToString()+"次在" + (mid + 1).ToString() + "中");
Console.ReadLine();
}
}