public void BinarySearch_SortedInput_ReturnsCorrectIndex()
        {
            int[] input = { 0, 3, 4, 7, 8, 12, 15, 22 };

            Assert.AreEqual(2, Searching.BinarySearch(input, 4));
            Assert.AreEqual(4, Searching.BinarySearch(input, 8));
            Assert.AreEqual(6, Searching.BinarySearch(input, 15));
            Assert.AreEqual(7, Searching.BinarySearch(input, 22));

            Assert.AreEqual(2, Searching.RecursiveBinarySearch(input, 4));
            Assert.AreEqual(4, Searching.RecursiveBinarySearch(input, 8));
            Assert.AreEqual(6, Searching.RecursiveBinarySearch(input, 15));
            Assert.AreEqual(7, Searching.RecursiveBinarySearch(input, 22));
        }
예제 #2
0
        public void BinarySearch_SortedInput_CorrectIndex()
        {
            int[]     input = { 0, 3, 4, 7, 8, 9, 12, 34, 56 };
            Searching s1    = new Searching(input, 4);
            Searching s2    = new Searching(input, 8);
            Searching s3    = new Searching(input, 12);
            Searching s4    = new Searching(input, 34);

            //Binary search tests
            //Normal search
            Assert.AreEqual(2, s1.BinarySearch());
            Assert.AreEqual(4, s2.BinarySearch());
            Assert.AreEqual(6, s3.BinarySearch());
            Assert.AreEqual(7, s4.BinarySearch());
            //Recursive Search
            Assert.AreEqual(2, s1.RecursiveBinarySearch());
            Assert.AreEqual(4, s2.RecursiveBinarySearch());
            Assert.AreEqual(6, s3.RecursiveBinarySearch());
            Assert.AreEqual(7, s4.RecursiveBinarySearch());
        }
        public void TestCustomBinarySearch()
        {
            var array = new CustomType[]
            {
                new CustomType(1, 2, "name1"),
                new CustomType(3, 4, "name2"),
                new CustomType(5, 6, "name3"),
                new CustomType(7, 8, "name4"),
                new CustomType(9, 10, "name5")
            };

            var smthToFind1 = new CustomType(1, 2, "name1");
            var smthToFind2 = new CustomType(5, 6, "name3");
            var smthToFind3 = new CustomType(9, 10, "name5");
            var smthToFind  = new CustomType(0, 0, "name0");

            Assert.True(Searching.BinarySearch(array, smthToFind1));
            Assert.True(Searching.BinarySearch(array, smthToFind2));
            Assert.True(Searching.BinarySearch(array, smthToFind3));
            Assert.False(Searching.BinarySearch(array, smthToFind));
        }
예제 #4
0
        private static void Main()
        {
            var generator = new Random();
            var data      = new int[15]; // create space for array

            // fill array with random ints in range 10-99
            for (var i = 0; i < data.Length; ++i)
            {
                data[i] = generator.Next(10, 100);
            }

            Array.Sort(data);                          // elements must be sorted in ascending order
            DisplayElements(data, 0, data.Length - 1); // display array

            // input first int from user
            Write($"{Environment.NewLine}Please enter an integer value (-1 to quit): ");
            var searchInt = int.Parse(ReadLine());

            // repeatedly input an integer; -1 terminates the app
            while (searchInt != -1)
            {
                // perform binary search
                var position = Searching.BinarySearch(data, searchInt);

                if (position != -1) // integer was found
                {
                    WriteLine($"The integer {searchInt} was found in " +
                              $"position {position}.\n");
                }
                else // integer was not found
                {
                    WriteLine(
                        $"The integer {searchInt} was not found.\n");
                }

                // input next int from user
                Write("Please enter an integer value (-1 to quit): ");
                searchInt = int.Parse(ReadLine());
            }
        }
 public void BinarySearching_CustomComparer()
 {
     Cat[] cats = new Cat[] { new Cat(1, "Кыса"), new Cat(1, "Кот"), new Cat(2, "Искомая") };
     Assert.AreEqual(2, Searching.BinarySearch(new Cat(6, "Искомая"), cats, new CustomCatsComparer()));
 }
 public void BinarySearching_CustomIComparable()
 {
     Cat[] cats = new Cat[] { new Cat(1, "Кыса"), new Cat(3, "Кыса2"), new Cat(2, "Искомая") };
     Assert.AreEqual(1, Searching.BinarySearch(new Cat(3, "Кыcа4"), cats));
 }
 public void BinarySearching_DefaultCompare(int f, int[] array, int expectedResult)
 {
     Assert.AreEqual(expectedResult, Searching.BinarySearch(f, array));
 }
 public bool TestStringBinarySearch(string[] array, string smthToFind)
 {
     return(Searching.BinarySearch(array, smthToFind));
 }
 public bool TestIntBinarySearch(int[] array, int smthToFind)
 {
     return(Searching.BinarySearch(array, smthToFind));
 }
예제 #10
0
 public void BinarySearchIsRightIndex(int[] array, int key, int expectedIndex)
 {
     Assert.AreEqual(expectedIndex, Searching.BinarySearch(array, key));
 }