Skip to content

Commit 7f13f15

Browse files
committed
binary_search_recur1.c
1 parent 0fd5f22 commit 7f13f15

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
int dfs(int nums[], int target, int i, int j) {
2+
if (i > j) {
3+
return -1;
4+
}
5+
6+
int m = (i + j) / 2;
7+
if(nums[m] < target) {
8+
return dfs(nums, target, m + 1, j);
9+
} else if (nums[m] > target) {
10+
return dfs(nums, target, i, m - 1);
11+
} else {
12+
return m;
13+
}
14+
15+
int binarySearch(int nums[], int target, int numsSize) {
16+
int n = numsSize;
17+
return dfs(nums, target, 0, n - 1);
18+
}
19+
}

0 commit comments

Comments
 (0)