Thursday, 21 November 2013

binary search without recursion

Binary search without recursion c program

Program:

#include<stdio.h>

void main()

{

   int a[50],n,i,key,f;

   clrscr();

   printf("enter no of elements to be entered");

   scanf("%d",&n);

   printf("enter elements ");

   for(i=1;i<=n;i++)

       scanf("%d",&a[i]);

   printf("enter the key to search");

   scanf("%d",&key);

   f=bsearch(a,n,key);

   if(f==0)

       printf("the key not found");

   else

       printf("the %d is found at %d",key,f);

   getch();

}

int bsearch(int a[],int n,int key)

{

   int low=1,high=n,mid;

   while(low<=high)

   {

        mid=(low+high)/2;

        if(key==a[mid])

            return mid;

        else if(key>a[mid])

            low=mid+1;

        else

            high=mid+1;

    }

return 0;

}

Output: 


1 comment:

  1. The casino's 125,000-square-foot gaming space is the
    The casino's 125,000-square-foot 김해 출장안마 gaming space is the 전라북도 출장마사지 largest of its kind in 의정부 출장샵 the country. The casino's 세종특별자치 출장마사지 500-square-foot gaming 서귀포 출장샵 space is the largest

    ReplyDelete