From 740cbf713a0a8b36358f03639e40ccf495abb708 Mon Sep 17 00:00:00 2001 From: kavvyaaa-a Date: Tue, 28 Oct 2025 22:25:11 +0530 Subject: [PATCH] update --- C/algorithms/searching/binary_search.c | 68 ++++++++++++++++---------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/C/algorithms/searching/binary_search.c b/C/algorithms/searching/binary_search.c index 0c69ed23..c3606360 100644 --- a/C/algorithms/searching/binary_search.c +++ b/C/algorithms/searching/binary_search.c @@ -11,32 +11,46 @@ */ #include -#define SIZE 10 -int BinarySearch(int [],int); -int main() -{ - int a[SIZE]={3,5,9,11,15,17,22,25,37,68},key,pos; - printf("Enter the Search Key\n"); - scanf("%d",&key); - pos=BinarySearch(a,key); - if(pos==-1) - printf("The search key is not in the array\n"); - else - printf("The search key %d is at location %d\n",key,pos); - return 0; +//function to perform binary search +int binarySearch(int arr[], int size, int key) { + int low = 0, high = size - 1, mid; + + while (low <= high) { + mid = (low + high) / 2; + + if (arr[mid] == key) + return mid; + else if (key < arr[mid]) + high = mid - 1; + else + low = mid + 1; } - int BinarySearch (int A[],int skey) - { - int low=0,high=SIZE-1,middle; - while (low <=high){ - middle=(low+high)/2; - if(skey==A[middle]) - return middle; - else if(skey