From 24a7208815f74094351535782095b6bb96e6a873 Mon Sep 17 00:00:00 2001 From: Akash2001 Date: Wed, 27 Oct 2021 20:11:00 +0530 Subject: [PATCH] Added binary_search.c --- binary_search.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 binary_search.c 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; +}