Pages

Showing posts with label Binery searching examples code. Show all posts
Showing posts with label Binery searching examples code. Show all posts

Write A program that perform Binary search for stored marks in Denz Array

INPUT:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication20
{     
    class Program
    {
        static void Main(string[] args)
        {
         int[] marks = { 11, 12, 14, 17, 19, 22, 29, 33, 36,39,40 };
int Left = -1, Right=8, mid,find;
foreach (int n in marks)
{
    Console.Write("{0}\t", n);
}
Console.WriteLine("\n\nserch the no");
find=Convert.ToInt32(Console.ReadLine());
while (Left + 1 != Right)
{
mid = (Left + Right) / 2;
if (marks[mid] <= find)
{
Left = mid;
}
else
{
    Right = mid;
}
}
if (Left >= 0 && marks[Left] == find)
{
Console.WriteLine("index  no : {0}",Left+ 1);
}
else
{
Console.WriteLine("NOt available");

}
        }
    }

 }
   



OUTPUT:


Make a program in which Using binary Search on Denz Array

CODE:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication20
{     
    class Program
    {
        static void Main(string[] args)
        {
            for( int i=0 ; i<1000; i++)
            {
                int  L = -1, R = 10, M;
            double[] MA = { 2.5, 3, 5.5, 7.5, 9.5, 11.5, 13.5, 15.5, 16, 17 };
            double N;
           
                Console.WriteLine("ENTER THE NUMBER:");
                N = Convert.ToDouble(Console.ReadLine());
                while (L + 1 != R)
                {
                    M = (L + R) / 2;
                    if (MA[M] <= N)
                    {
                        L = M;
                    }
                    else
                    {
                        R = M;
                    }
                }
                if ((L >= 0) && (MA[L] == N))
                {
                    Console.WriteLine(" {0} EXIST AT {1} INDEX NUMBER.", N, L + 1);
                }
                else
                {
                    Console.WriteLine(" {0} DOES NOT EXIST.", N);
                }
            }
        }
    }

}

OUTPUT:


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: