Example #1
0
    public int Search(ArrayReader reader, int target)
    {
        int left  = 0;
        int right = 1;

        while (reader.Get(right) < target)
        {
            right *= 2;
        }
        while (left < right - 1)
        {
            int mid = left + (right - left) / 2;
            if (target == reader.Get(mid))
            {
                return(mid);
            }
            if (target < reader.Get(mid))
            {
                right = mid - 1;
            }
            else
            {
                left = mid + 1;
            }
        }
        if (target == reader.Get(left))
        {
            return(left);
        }
        if (target == reader.Get(right))
        {
            return(right);
        }
        return(-1);
    }
Example #2
0
        public int Search(ArrayReader reader, int target)
        {
            int left = 0, right = 1;

            while (reader.Get(right) < target)
            {
                left    = right;
                right <<= 1;
            }
            while (left <= right)
            {
                var mid   = left + (right - left) / 2;
                var value = reader.Get(mid);
                if (value == target)
                {
                    return(mid);
                }
                else if (value > target)
                {
                    right = mid - 1;
                }
                else
                {
                    left = mid + 1;
                }
            }

            return(-1);
        }
    public int BruteForce(ArrayReader reader, int target)
    {
        int left = 0, right = 10000;

        while (left <= right)
        {
            int mid = left + (right - left) / 2;

            int element = reader.Get(mid);

            if (element == Int32.MaxValue)
            {
                right = mid - 1;
                continue;
            }

            if (element == target)
            {
                return(mid);
            }
            else if (element < target)
            {
                left = mid + 1;
            }
            else
            {
                right = mid - 1;
            }
        }

        return(-1);
    }
Example #4
0
        public static int Search(ArrayReader reader, int key)
        {
            // TODO: Write your code here

            int tempEnd   = 1;
            int tempStart = 0;

            while (reader.Get(tempEnd) < key)
            {
                tempStart = tempEnd;
                tempEnd  *= 2;

                if (reader.Get(tempEnd) == key)
                {
                    return(tempEnd);
                }
            }

            return(BinarySearch(reader, key, tempStart, tempEnd));
        }
    public int Search(ArrayReader reader, int target)
    {
        int left = 0, right = 1;

        while (reader.Get(right) < target)
        {
            left   = right;
            right *= 2;
        }

        // [-1, 0, 3, 5, 9, 12]     target = 9
        //   l  r
        //      l  r
        //         l     r
        //               l             r
        // To keep logarithmic time complexity, let's extend it twice as far.
        // If the reader[right] < target, then we can ignore all left side of the "right" index and move the search boundary to the right.

        while (left <= right)
        {
            int mid     = left + (right - left) / 2;
            int element = reader.Get(mid);
            if (element == target)
            {
                return(mid);
            }
            else if (element < target)
            {
                left = mid + 1;
            }
            else
            {
                right = mid - 1;
            }
        }

        return(-1);
    }
Example #6
0
        private static int BinarySearch(ArrayReader reader, int key, int start, int end)
        {
            while (start <= end)
            {
                int mid = start + (end - start) / 2;

                if (reader.Get(mid) == key)
                {
                    return(mid);
                }

                if (reader.Get(mid) > key)
                {
                    end = mid - 1;
                }
                else
                {
                    start = mid + 1;
                }
            }

            return(-1);
        }