From fd9236245c722ba19a349475f356b0202738a712 Mon Sep 17 00:00:00 2001 From: neha510 <84696537+neha510@users.noreply.github.com> Date: Sun, 1 Oct 2023 18:34:47 +0530 Subject: [PATCH] Create Binary search --- searching/Binary search | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 searching/Binary search diff --git a/searching/Binary search b/searching/Binary search new file mode 100644 index 00000000..734f83ef --- /dev/null +++ b/searching/Binary search @@ -0,0 +1,29 @@ +#include +int main() +{ +int i, low, high, mid, n, key, array[100]; +printf("Enter number of elementsn"); +scanf("%d",&n); +printf("Enter %d integersn", n); +for(i = 0; i < n; i++) +scanf("%d",&array[i]); +printf("Enter value to findn"); +scanf("%d", &key); +low = 0; +high = n - 1; +mid = (low+high)/2; +while (low <= high) { +if(array[mid] < key) +low = mid + 1; +else if (array[mid] == key) { +printf("%d found at location %d.n", key, mid+1); +break; +} +else +high = mid - 1; +mid = (low + high)/2; +} +if(low > high) +printf("Not found! %d isn't present in the list.n", key); +return 0; +}