-
Notifications
You must be signed in to change notification settings - Fork 1
/
binarySearch.c
59 lines (47 loc) · 1.11 KB
/
binarySearch.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include <conio.h>
int binarySearch(int arr[], int left, int right, int num)
{
if (right >= left)
{
int mid = left + (right - left) / 2;
if (arr[mid] == num)
return mid + 1;
if (arr[mid] > num)
return binarySearch(arr, left, mid - 1, num);
return binarySearch(arr, mid + 1, right, num);
}
return -1;
}
int main()
{
int arr[10] = {10, 12, 20, 21, 22, 50, 52, 55, 82, 100};
int num;
int pos = -1;
printf("Original array: ");
for (int i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
printf("\nEnter an element to be searched: ");
scanf("%d", &num);
pos = binarySearch(arr, 0, 9, num);
if (pos != -1)
{
printf("%d found at position %d\n", num, pos);
}
else
{
printf("%d not found in the array\n", num);
}
return 0;
}
/*
Output:
Original array: 10 12 20 21 22 50 52 55 82 100
Enter an element to be searched: 58
58 not found in the array
Original array: 10 12 20 21 22 50 52 55 82 100
Enter an element to be searched: 55
55 found at position 8
*/