diff --git a/binary_search.c b/binary_search.c new file mode 100644 index 0000000..0eeb17d --- /dev/null +++ b/binary_search.c @@ -0,0 +1,60 @@ +#include +#include + +int main() +{ + int n,data,upper_bound,lower_bound,middle; + char choice='Y'; + + printf("Enter number of elements to be filled in array : "); + scanf("%d",&n); + + int *arr=(int*)malloc(n*sizeof(int)); + + printf("Enter elements of array in ascending order\n"); + + for(int i=0;iarr[middle]) + { + lower_bound=middle+1; + middle=upper_bound+lower_bound; + } + else + { + upper_bound=middle-1; + middle=upper_bound+lower_bound; + } + } + if(arr[middle]==data) + { + printf("Element found at %d\n",middle); + } + else + { + printf("Element not found !\n"); + } + + fflush(stdin); + + printf("Press y to continue or press other keys to exit : "); + scanf("%c",&choice); + } + + return 0; +}