Skip to content

Commit cf481f9

Browse files
authored
Merge pull request #2 from Omi-code404/add-binary-search-1
feat: implement recursive binary search in C
2 parents 7e5009b + fdf7bbd commit cf481f9

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

binary_search_function_code.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
//বাইনারি সার্চের মূল উদ্দেশ্য হলো টার্গেটের ইনডেক্স খুঁজে বের করা
3+
4+
int binary_search(int arr[], int left, int right, int target)
5+
6+
{
7+
if(left > right) return -1;
8+
9+
int mid = left + (right - left) / 2;
10+
11+
12+
if (arr[mid] == target)
13+
return mid;
14+
else if (arr[mid] > target)
15+
return binary_search(arr, left, mid-1, target);
16+
else
17+
return binary_search(arr, mid+1, right, target);
18+
}
19+
20+
int main() {
21+
22+
int arr[] = {2, 5, 8, 12, 16, 25, 38, 56, 72, 91};
23+
24+
int target = 38;
25+
26+
int index = binary_search(arr, 0, 9, target);
27+
28+
29+
if(index != -1)
30+
31+
printf("%d Find it in the index %d\n", target, index);
32+
33+
else
34+
35+
printf("%d can not find it \n", target);
36+
37+
return 0;
38+
}

0 commit comments

Comments
 (0)