-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search.c
57 lines (44 loc) · 1.37 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
// Function to perform binary search
int binarySearch(int arr[], int n, int target) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
// Check if target is at mid
if (arr[mid] == target) {
return mid; // Target found at index mid
}
// If target is greater, ignore the left half
if (arr[mid] < target) {
left = mid + 1;
} else {
// If target is smaller, ignore the right half
right = mid - 1;
}
}
return -1; // Target is not present in the array
}
int main() {
int n, target;
// Input number of elements in the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
// Input elements of the array
printf("Enter %d sorted elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Input the target value to search
printf("Enter the value to search: ");
scanf("%d", &target);
// Perform binary search
int result = binarySearch(arr, n, target);
// Output the result
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found in the array\n");
}
return 0;
}