From 297e2e44bba3a533424e45abcae30a0726a31c07 Mon Sep 17 00:00:00 2001 From: Akash Barman <92024930+itstheakash@users.noreply.github.com> Date: Wed, 26 Oct 2022 15:16:48 +0530 Subject: [PATCH] Create Binary Searching --- Binary Searching | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Binary Searching diff --git a/Binary Searching b/Binary Searching new file mode 100644 index 0000000..92829e9 --- /dev/null +++ b/Binary Searching @@ -0,0 +1,43 @@ +#include + +// Binary Searching with C Language + +void main() +{ + int i, n, item, a[100]; + printf("Enter the size of the array: "); + scanf("%d", &n); + printf("Enter the element of the array: "); + for(i = 0; i < n; i++) + { + scanf("%d", &a[i]); + } + + printf("Enter the item to be searched: "); + scanf("%d", &item); + + int mid, beg = 0, end = n -1; + + while(beg <= end) + { + mid = (beg + end) / 2; + if(a[mid] == item) + { + printf("The item is present at position: %d", mid); + break; + } + else if(a[mid] < item) + { + beg = mid + 1; + } + else + { + end = mid - 1; + } + } + + if(beg > end) + { + printf("Item not found!"); + } +}