Pages

WRITE A PROGRAMME TO FIND A NUM BY BINARY SEARCH: USING DENZ ARRAY

INPUT:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Denz_Array
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\t\t\t\tmARKXX");
            Console.WriteLine("");
            Console.WriteLine("");

            int[] Marks = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

            int L = -1, R = 10, m;
            int n;

            Console.WriteLine("Enter your Mraks");
            n = Convert.ToInt32(Console.ReadLine());

            while (L + 1 != R)
            {
                m = (L + R) / 2;
                if (Marks[m] <= n)
                {
                    L = m;
                }
                else
                {
                    R = m;
                }
            }

            if ((L >= 0) && (Marks[L] == n))
            {
                Console.WriteLine("{0} exist at index number {1}", n, L + 1);
            }
            else
            {
                Console.WriteLine("{0} does not exist", n);
            }

        }
    }

}

Output:


No comments:

Post a Comment