/// <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);
            }
        }
예제 #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);
        }
예제 #3
0
 public void ArraySearchSortedNoSize2BinarySearchCompleteTestNegativeIndex()
 {
     try
     {
         var listy = new Listy(new[] { 3, 5, 7, 7, 8, 23, 38, 56 });
         ArraySearchSortedNoSize2BinarySearchComplete.Search(listy, -2);
     }
     catch (ArgumentException)
     {
         Assert.IsTrue(true);
         return;
     }
     Assert.Fail();
 }
        /// <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);
        }
예제 #5
0
        public void ArraySearchSortedNoSize2BinarySearchCompleteTestEmptyLists()
        {
            var listy = new Listy(new int[0]);

            Assert.AreEqual(-1, ArraySearchSortedNoSize2BinarySearchComplete.Search(listy, 23));
        }
예제 #6
0
        public void ArraySearchSortedNoSize2BinarySearchCompleteTestOneElementNotFound()
        {
            var listy = new Listy(new[] { 3 });

            Assert.AreEqual(-1, ArraySearchSortedNoSize2BinarySearchComplete.Search(listy, 23));
        }
예제 #7
0
        public void ArraySearchSortedNoSize2BinarySearchCompleteTestValueNotFound()
        {
            var listy = new Listy(new[] { 3, 5, 7, 7, 8, 23, 38, 56 });

            Assert.AreEqual(-1, ArraySearchSortedNoSize2BinarySearchComplete.Search(listy, 100));
        }
        public void ArraySearchSortedNoSize1BruteForceCompleteTestOneElementFound()
        {
            var listy = new Listy(new[] { 3 });

            Assert.AreEqual(0, ArraySearchSortedNoSize1BruteForceComplete.Search(listy, 3));
        }
        public void ArraySearchSortedNoSize1BruteForceCompleteTestValueFound2()
        {
            var listy = new Listy(new[] { 3, 5, 7, 7, 8, 23, 38, 56 });

            Assert.AreEqual(5, ArraySearchSortedNoSize1BruteForceComplete.Search(listy, 23));
        }
        public void ArraySearchSortedNoSize3BinarySearchNoSizeCompleteTestValueFound1()
        {
            var listy = new Listy(new[] { 3, 5, 7, 7, 8, 23, 38, 56 });

            Assert.AreEqual(4, ArraySearchSortedNoSize3BinarySearchNoSizeComplete.Search(listy, 8));
        }