#include <stdio.h>
#include <stdlib.h>
#define N 5
binary_search(int arr[], int n, int key)
{
int low = 0;
int high = n-1;
while(low < high)
{
int mid = (low + high)/2;
int midval = arr[mid];
if(midval < key)
low = mid + 1;
else if(midval > key)
high = mid - 1;
else
return mid;
}
return -1;
}
int main()
{
int i,val,ret;
int arr[N] = {1,3,5,7,9};
for(i=0;i<N;i++)
{
printf("%d\t",arr[i]);
}
printf("\n");
printf("Please enter the value you want to search:\n");
scanf("%d",&val);
ret = binary_search(arr,N,val);fflush(stdout);
if(-1 == ret)
printf("The value is not existed!\n");
else
printf("Successfully search the value!\n");
return 0;
}