Beispiel #1
0
        static void Main(string[] args)
        {
            var reader = new ArrayReader(new[] { -1, 0, 3, 5, 9, 12 });
            int target = 2;

            Console.WriteLine(Search(reader, target));
        }
Beispiel #2
0
        static int Search(ArrayReader reader, int target)
        {
            int li = 0, hi = 10000;

            while (li < hi)
            {
                int mid = li + (hi - li) / 2;
                int num = reader.Get(mid);
                if (num == target)
                {
                    return(mid);
                }
                if (num > target || num == -1)
                {
                    hi = mid;
                }
                else
                {
                    li = mid + 1;
                }
            }
            return(reader.Get(li) == target ? li : -1);
        }