This repository was archived by the owner on Sep 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 614
Expand file tree
/
Copy pathbinary_search.c
More file actions
50 lines (40 loc) · 1.47 KB
/
binary_search.c
File metadata and controls
50 lines (40 loc) · 1.47 KB
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
#include <stdio.h>
// Function to perform binary search on a sorted array
int binarySearch(int arr[], int size, int target) {
// Initializing the left and right pointers for the search range
int left = 0, right = size - 1;
// Loop continues as long as the search range is valid (left <= right)
while (left <= right) {
// Calculate the mid index to avoid overflow
int mid = left + (right - left) / 2;
// Check if the target is found at the mid index
if (arr[mid] == target) {
return mid; // Target found, return the index
}
// If target is greater than mid element, search the right half
if (arr[mid] < target) {
left = mid + 1; // Narrow the search to the right part
}
// If target is smaller than mid element, search the left half
else {
right = mid - 1; // Narrow the search to the left part
}
}
// If the target is not found, return -1
return -1;
}
int main() {
// Example sorted array
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
// Target element to search for
int target = 5;
// Call binarySearch function and store the result
int result = binarySearch(arr, sizeof(arr) / sizeof(arr[0]), target);
// Print the result based on whether the target was found
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}