Name: MUHEET RASHID DIV- D7B ROLL NO. 43 A.Y.
-2019-20
SUBJ.- DATA STRUCTURES
BINARY SEARCH
PROGRAM CODE:
#include<stdio.h>
void main()
int a[10],n,i,k,mid,u,l,c=0;
printf("¥nEnter the no. of elements of array: ");
scanf("%d",&n);
printf("¥nEnter the elements:");
for(i=0;i<n;i++)
printf("¥n Enter element %d : ",i+1);
scanf("%d",&a[i]);
printf("¥n Enter the element to be searched: ");
scanf("%d",&k);
u=n-1;
l=0;
while(u-l>=0)
mid=(u+l)/2;
if(k==a[mid])
{
printf("¥nElement found at index: %d",mid);
c++;
break;
else if(k>a[mid])
l=mid+1;
else
u=mid-1;
OUTPUT:
Enter the no. of elements of array: 6
Enter the elements:
Enter element 1 : 12
Enter element 2 : 23
Enter element 3 : 34
Enter element 4 : 45
Enter element 5 : 56
Enter element 6 : 67
Enter the element to be searched: 67
Element found at index: 5
Process returned 2686708 (0x28FEF4) execution time : 11.591 s
Press any key to continue.