Programme for Binary search

Programme for Binary search

#include<stdio.h>
#include<conio.h>
int arr[20];
int x;
void input()
{
int i;
printf("How many elements out of 20:\n");
scanf("%d",&x);
for(i=0;i<x;i++)
 {
    printf("Enter no.=");
    scanf("%d",&arr[i]);
 }
}
void search()
{
    int n,i,mid,low,high,flag=0;
    printf("Enter no which you want to search: \n");

    scanf("%d",&n);
    low=0;
    high=x-1;
    while(high>=low)
    {
    mid=(low+high)/2;
        if(n>arr[mid])
        {
        low=mid+1;
        }
        else if(n<arr[mid])
        {
        high=mid-1;
        }
        else
        {
        flag=1;
        break;
        }
    }
if(flag==1)
printf("Number found");
else
printf("Number not found");
}
void main()
{
clrscr();
input();
search();
getch();
}

SHARE

Ramandeep Singh

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 $type={blogger}:

Post a Comment