Beispiel #1
0
Datei: Search.cs Projekt: ioab/AU
        public static void Demo()
        {
            var s = new Searching();

            //Console.WriteLine(s.LinearSearch(new[] { 100, 10, 93, 15 }, 4, 15));
            //Console.WriteLine(s.LinearSearch(new[] { "Eve", "Sam", "Bell", "Kal" }, 4, "Eve"));

            //Console.WriteLine(s.BetterLinearSearch(new[] { 50, 540.54, 110.258, 0, 47.26 }, 5, 110.258));

            //Console.WriteLine(s.SentinelLinearSearch(new[] { "100", "af", "1" }, 3, "100"));

            //Console.WriteLine(s.RecursiveLinearSearch(new[] { 50, 540.54, 110.258, 0, 47.26 }, 5, 110.258));
            //Console.WriteLine(s.RecursiveLinearSearch(new[] { "100", "af", "1" }, 3, "se"));

            //Console.WriteLine(s.BinarySearch(new[] { 0, 47.26, 50, 54.54, 110.258, 400 }, 6, 110.258));
            //Console.WriteLine(s.BinarySearch(new[] { "avb", "ade", "bcs", "zx" }, 4, "bcs"));

            Console.WriteLine(s.RecursiveBinarySearch(new[] { 0, 47.26, 50, 54.54, 110.258, 400 }, 0, 6, 110.258));
            Console.WriteLine(s.RecursiveBinarySearch(new[] { "avb", "ade", "bcs", "zx" }, 0, 4, "bcs"));
        }
        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));
        }
Beispiel #3
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());
        }