Example #1
0
        public static bool GallopingSearch(int[] array, int N)
        {
            if (array == null || array.Length == 0)
            {
                return(false);
            }

            int i     = 1;
            int index = Convert.ToInt32(Math.Pow(2, i)) - 2;

            while (array[index] <= N && index < array.Length - 1)
            {
                if (array[index] == N)
                {
                    return(true);
                }

                if (array[index] < N)
                {
                    ++i;
                    index = Convert.ToInt32(Math.Pow(2, i)) - 2;

                    if (index >= array.Length - 1)
                    {
                        index = array.Length - 1;
                    }
                }
            }

            BinarySearch bs = new BinarySearch(array)
            {
                Right = index,
                Left  = Convert.ToInt32(Math.Pow(2, i - 1)) - 2 + 1
            };

            while (bs.GetResult() == 0)
            {
                bs.Step(N);
            }

            return(bs.GetResult() == 1);
        }
Example #2
0
        //
        // Бинарный поиск "От края"
        //
        public static bool GallopingSearch(int[] array, int N)
        {
            if (N < array[0] || N > array[array.Length - 1])
            {
                return(false);
            }

            BinarySearch BS = new BinarySearch(array);

            for (int i = 1; i <= array.Length - 1; i++)
            {
                int index = (int)(Math.Pow(2, i) - 2);
                if (index > array.Length - 1)
                {
                    index = array.Length - 1;
                }
                if (array[index] == N)
                {
                    return(true);
                }

                if (array[index] < N)
                {
                    continue;
                }
                else if (array[index] > N)
                {
                    BS.Left  = (int)(Math.Pow(2, i - 1) - 2) + 1;
                    BS.Right = index;
                }
                break;
            }

            while (BS.GetResult() == 0)
            {
                BS.Step(N);
            }
            return(BS.GetResult() == 1);
        }