0% found this document useful (0 votes)
17 views2 pages

Exp 2 B

Y

Uploaded by

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

Exp 2 B

Y

Uploaded by

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

/*--- Binary Search---*/

#include<stdio.h>
#include<conio.h>
void main()
{
int x,k,arr[100],i,j, high,low,mid,found=0;
clrscr();
printf("\nEnter number of array element (less than 100):");
scanf("%d",&x);
for(i=0;i<x;i++)
{
printf("Enter element number%d\t",i+1);
scanf("%d",&arr[i]);
}
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
if(arr[j]>arr[j+1])
{
int t;
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
for(i=0;i<x;i++)
{
printf("\nElement number%d\n",arr[i]);
}

printf("\n Enter element to be searched");


scanf("%d",&k);
low=0 ;
high=x-1;
while(low<=high)
{
mid=(low+high)/2;
if(arr[mid]==k)
{
printf("\nElement%d found at position %d",k,mid+1);
found=1;
break;
}
else
{
if(arr[mid]>k)
high=mid-1;
else
low=mid+1;
}
}
if(found!=1)
{
printf("Not found");
}
getch();
}

/*-----OUTPUT -----*/
/*
Enter number of array element (less than 100):4
Enter element number1 23
Enter element number2 45
Enter element number3 98
Enter element number4 12

Enter element to be searched12

Element12 found at position 1

*/

You might also like