46 lines
1.1 KiB
C++
46 lines
1.1 KiB
C++
|
/* Program to demonstrate non recursive binary search */
|
||
|
|
||
|
/* We assume that the array is sorted in ascending order */
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <conio.h>
|
||
|
|
||
|
int binary(int x[ ], int n, int key) ;
|
||
|
|
||
|
void main()
|
||
|
{
|
||
|
int i, n, x[20], key ;
|
||
|
clrscr() ;
|
||
|
printf("Enter the number of elements: ") ;
|
||
|
scanf("%d",&n) ;
|
||
|
printf("Enter the elements:\n") ;
|
||
|
for(i=0 ; i<n ; i++)
|
||
|
scanf("%d",&x[i]) ;
|
||
|
printf("Enter the element to be searched: ") ;
|
||
|
scanf("%d",&key) ;
|
||
|
i=binary(x,n,key) ;
|
||
|
if(i==-1)
|
||
|
printf("%d not found.", key) ;
|
||
|
else
|
||
|
printf("%d found at position %d.", key,i) ;
|
||
|
getch() ;
|
||
|
}
|
||
|
|
||
|
int binary(int x[ ], int n, int key)
|
||
|
{
|
||
|
int low, high, mid ;
|
||
|
low=0 ;
|
||
|
high=n-1 ;
|
||
|
while(low<=high)
|
||
|
{
|
||
|
mid=(low+high)/2 ;
|
||
|
if(key==x[mid])
|
||
|
return mid ;
|
||
|
else if(key<x[mid])
|
||
|
high=mid-1 ;
|
||
|
else
|
||
|
low=mid+1 ;
|
||
|
}
|
||
|
return -1 ;
|
||
|
}
|
||
|
/* The condition (key<x[mid]) will become (key>x[mid]) if array x is in descending order */
|