forked from NITSkmOS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include<stdio.h> | ||
#define MAX 100 | ||
void main() | ||
{ | ||
int array[MAX],l,r,mid,n,x,flag=0,i; | ||
printf("enter the no of elements\n"); | ||
scanf("%d", &n); | ||
printf("enter the elements of the sorted array\n"); | ||
for(i=0;i<n;i++) | ||
{scanf("%d",&array[i]); | ||
} | ||
printf("enter the element to be searched\n"); | ||
scanf("%d", &x); | ||
l=0; | ||
r=n-1; | ||
mid=(l+r)/2; | ||
while(l<r) | ||
{if(x==array[mid]) | ||
{printf("\nthe element is at pos %d\n",mid); | ||
flag=1; | ||
break; | ||
} | ||
else if(x<array[mid]) | ||
{r=mid-1; | ||
mid=(l+r)/2; | ||
} | ||
else if(x>array[mid]) | ||
{l=mid+1; | ||
mid=(l+r)/2; | ||
} | ||
|
||
} | ||
if(flag==0) | ||
printf("The element is not found\n"); | ||
|
||
} |