forked from Anuragcoderealy/Codenewhacktober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search.c
33 lines (33 loc) · 1.04 KB
/
binary_search.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int n, i, x, flag = 0;
printf("Enter the number of elements in the array:\n");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the element to be searched: ");
scanf("%d", &x);
int start = 0, end = n - 1; // searching starts from start variable to end variable
while (start <= end) // loop for searching
{
int mid = start + (end - start) / 2; // calculating middle element of the search
if (arr[mid] == x) // if search element is found
flag = mid;
if (arr[mid] > x) // if mid element is greater than element to be searched
end = mid - 1;
else // if mid element is smaller than element to be searched
start = mid + 1;
}
if (flag == 0)
printf("Element not found");
else
printf("Element found at index %d", flag);
getch();
}