Skip to content

Commit 91c6f3c

Browse files
Create IterativeBinarySearch.c
1 parent c661f05 commit 91c6f3c

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

IterativeBinarySearch.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<stdio.h>
2+
void main()
3+
{
4+
int mid,lb,ub,i,n,key;
5+
printf(" Enter The Size of 1D Array\n");
6+
scanf(" %d", &n);
7+
int arr[n];
8+
printf(" Enter The Elements into the Array: \n");
9+
for(i=0;i<n;i++)
10+
scanf("%d",&arr[i]);
11+
printf("\n\n The Given Array Is:\n \n");
12+
for(i=0;i<n;i++)
13+
printf("%d ",arr[i]);
14+
printf("\n Enter the number to be searched:");
15+
scanf("%d", &key);
16+
lb=0;
17+
ub=n-1;
18+
mid=(lb+ub)/2;
19+
while(lb<=ub)
20+
{
21+
if(key==arr[mid])
22+
{
23+
printf("\n %d found at %d",key,(mid+1));
24+
break;
25+
}
26+
else if(key<arr[mid])
27+
ub=mid-1;
28+
else if(key>arr[mid])
29+
lb=mid+1;
30+
else
31+
{
32+
printf("\n %d not found in the array", key);
33+
break;
34+
}
35+
mid=(lb+ub)/2;
36+
}
37+
}
38+

0 commit comments

Comments
 (0)