public void TestBinarySearch()
        {
            // The first item is a sorted array, the second item is the
            // value to search for and the third item is the expected index
            // returned by the search method.
            Tuple <int[], int, int>[] test_inputs =
            {
                new Tuple <int[], int, int>(
                    new int[] { },
                    7,            -1),

                new Tuple <int[], int, int>(
                    new int[] { 5 },
                    -5,           -1),

                new Tuple <int[], int, int>(
                    new int[] { 5 },
                    5,             0),

                new Tuple <int[], int, int>(
                    new int[] { -5,-3,   0,   1,   5,    9, 14, 90, 93 },
                    93,            8),

                new Tuple <int[], int, int>(
                    new int[] { 0,  1,   2,   3,   4,    5,  6,7 },
                    8,            -1),

                new Tuple <int[], int, int>(
                    new int[] { -4,-2,   0,   2,   5,    7, 10,99 },
                    0,             2),

                new Tuple <int[], int, int>(
                    new int[] { 8, 90, 100, 345, 598, 1054,3929 },
                    400, -1)
            };

            foreach (var test_input in test_inputs)
            {
                Assert.AreEqual(
                    test_input.Item3,
                    RecursiveBinarySearch <int> .Search(test_input.Item1, test_input.Item2));
            }
        }
Beispiel #2
0
        private void btnExecute_Click(object sender, EventArgs e)
        {
            RecursiveBinarySearch rbs = new RecursiveBinarySearch();
            int value = -1;

            int[] ADSMarks = new int[] { 72, 55, 1, 61, 38, 96, 45, 73, 57, 39, 6, 88 };

            Array.Sort(ADSMarks);

            txtOutput.Text += "\r\nSorted array: \r\n";
            foreach (int i in ADSMarks)
            {
                txtOutput.Text += i + "\t";
            }
            value = rbs.RecursiveBinarySearch1(ADSMarks, 0, ADSMarks.Length, 61);
            if (value != -1)
            {
                txtOutput.Text += "\r\nValue 61 Found!!\r\n" + "Index of the value : " + value;
            }
            else
            {
                txtOutput.Text += "\r\nValue not found.";
            }
        }