0% found this document useful (0 votes)
15 views1 page

2

This document contains a C program that implements a binary search algorithm. It prompts the user to input the number of elements in an array, the elements themselves, and a key to search for. The program then searches for the key in the array and outputs its position or indicates if the element is not found.

Uploaded by

swetha.v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views1 page

2

This document contains a C program that implements a binary search algorithm. It prompts the user to input the number of elements in an array, the elements themselves, and a key to search for. The program then searches for the key in the array and outputs its position or indicates if the element is not found.

Uploaded by

swetha.v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include<stdio.

h>

int main()
{
int i,n,key,low,high,mid;
printf("enter the no of elements\n");
scanf("%d",&n);
int array[n];
printf("enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&array[i]);
printf("enter the key\n");
scanf("%d",&key);
low=0;
high=n-1;
mid=(low+high)/2;

while(low<=high)
{
if(array[mid]<key)
low=mid+1;
else if(array[mid]==key)
{
printf("%d found at %d",key,mid+1);
break;
}
else
high=mid-1;
mid=(low+high)/2;
}
if(low>high)
{
printf("element not found");
}
}

You might also like