/// <summary>
        /// Search for value in listy.
        /// </summary>
        /// <param name="listy">The list.</param>
        /// <param name="value">The value to find.</param>
        /// <returns>The index of the value or -1 if not found.</returns>
        public static int Search(Listy listy, int value)
        {
            // Error checking.
            if (listy == null)
            {
                throw new ArgumentException("Both arrays must be non-null");
            }

            // Error checking.
            if (value < 0)
            {
                throw new ArgumentException("Value must be positive.");
            }

            var index   = 0;
            var element = listy.ElementAt(index);

            while (element < value && element != -1)
            {
                index++;
                element = listy.ElementAt(index);
            }

            if (element == -1)
            {
                return(-1);
            }
            else
            {
                return(index);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Search for value in listy.
        /// </summary>
        /// <param name="listy">The list.</param>
        /// <param name="value">The value to find.</param>
        /// <returns>The index of the value or -1 if not found.</returns>
        public static int Search(Listy listy, int value)
        {
            // Error checking.
            if (listy == null)
            {
                throw new ArgumentException("Both arrays must be non-null");
            }

            // Error checking.
            if (value < 0)
            {
                throw new ArgumentException("Value must be positive.");
            }

            // Find starting index.
            var index   = 1;
            var element = listy.ElementAt(index);

            while (element != -1 && element < value)
            {
                index  *= 2;
                element = listy.ElementAt(index);
            }

            // Initialize indexes.
            var indexStart = index / 2;
            var indexEnd   = index;

            // At this point we know that value is between indexStart and indexEnd.
            // Do a standard binary search.
            while (indexEnd >= indexStart)
            {
                // Get middle element.
                var middle = ((indexEnd - indexStart) / 2) + indexStart;
                element = listy.ElementAt(middle);

                // Go left.
                if (element == -1 || value < element)
                {
                    indexEnd = middle - 1;
                }
                else if (value > element)
                // Go right.
                {
                    indexStart = middle + 1;
                }
                // Match.
                else
                {
                    return(middle);
                }
            }

            // Not found.
            return(-1);
        }
        /// <summary>
        /// Search for value in listy.
        /// </summary>
        /// <param name="listy">The list.</param>
        /// <param name="value">The value to find.</param>
        /// <returns>The index of the value or -1 if not found.</returns>
        public static int Search(Listy listy, int value)
        {
            // Error checking.
            if (listy == null)
            {
                throw new ArgumentException("Both arrays must be non-null");
            }

            // Error checking.
            if (value < 0)
            {
                throw new ArgumentException("Value must be positive.");
            }

            // Note: Important that index starts at zero to handle case where
            // matching element is first element in array.
            var indexPrev = 0;
            var index     = 0;
            int?indexLast = null;

            // Outer loop, unknown length binary search.
            // Note: End point is
            // indexPrev == indexLast (First element is element we are looking for)
            // or indexPrev == indexLast - 1 (Element not found).
            while (indexLast == null || (indexPrev < indexLast - 1))
            {
                // Start increment at 1.
                var indexIncrement = 1;

                // Get current element.
                var element = listy.ElementAt(index);

                // Inner loop find element or outside of array bounds.
                while (element != -1 && element < value)
                {
                    // Store previous index.
                    indexPrev = index;

                    // Update the index and element.
                    index  += indexIncrement;
                    element = listy.ElementAt(index);

                    // Update the increment.
                    indexIncrement *= 2;
                }

                // Found element.
                if (element == value)
                {
                    return(index);
                }

                // Store last index.
                indexLast = index;

                // Reset starting location.
                index = indexPrev;
            }

            // Not found.
            return(-1);
        }