using System;
class BinarySearch
{
public int search(int[] A, int p, int q, int key)
{
int result = -1;
if (p >= q && key == A[p])
{
result = p;
}
else
{
int k = (p+q)/2;
if (key == A[k])
result = k;
else
result = (key < A[k]) ? search(A, p, k-1, key) : search(A, k+1, q, key);
}
return result;
}
}
class Program
{
public static void Main()
{
Random rnd = new Random();
int[] data = new int[10];
for (int i=0; i<10; i++)
{
data[i] = i;
Console.Write("{0}/t",data[i]);
}
Console.WriteLine();
BinarySearch s = new BinarySearch();
int t = s.search(data, 0, 9, 5);
Console.Write(t);
}
}
二分法在C#.net 中的应用
最新推荐文章于 2021-12-11 22:00:08 发布