二分法查找
class Data {
    public int id;
    public string Name;
}

class Program
{
    static Dictionary<int,Data> list;

    static void Main(string[] args)
    {
        list = new Dictionary<int, Data>();
        for (int i = 1; i <= 100; i++) {
            Data data = new Data();
            data.id = i;
            data.Name = "Name" + i;
            list.Add(i,data);
        }

        while (true) {
            Console.WriteLine("输入你要查找的数字");
            string input = Console.ReadLine();
            int num;
            Int32.TryParse(input, out num);
            int index = searchNum(num, 1, list.Count);
            Console.WriteLine(index);
        }
    }

    static int searchNum(int value, int from, int to) {

        int result = -1;

        int temp = (int)Math.Floor((double)(from + to) / 2);

        if (temp < 1 || temp > list.Count) {
            return -1;
        }

        //获取中间元素的值
        int tempVal = list[temp].id;
        if (tempVal == value) {
            return temp;
        }

        if (from == to)  {//找不到
            return -1;
        }

        if (tempVal > value) {
            result = searchNum(value, from, temp-1);
        }

        if (tempVal < value) {
            result = searchNum(value, temp+1, to);
        }

        return result;
    }
}


运行时间为:O(log(n))

对于一个已经排好序的列表,使用中间值的方式来比较值

如果小了,就往左边查找,缩小一半的范围

同样的这样循环,一直对半缩小范围,直到查找到值或找不到(范围内只有一个值了)


首页 我的博客
粤ICP备17103704号