-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary search.c
More file actions
36 lines (29 loc) · 906 Bytes
/
binary search.c
File metadata and controls
36 lines (29 loc) · 906 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void binary_search(int arr [], int st, int ed, int target);
int main(){
int arr[] = {7, 9, 10, 23, 45, 76, 90, 102};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 90;
printf("Array is : ");
for (int i = 0; i < n; i ++){
printf("%d ", arr[i]);
}
printf("\n--------------\n");
printf("Target is : %d", target);
printf("\n--------------\n");
binary_search(arr, 0, n - 1, target);
}
void binary_search(int arr [], int st, int ed, int target){
if (ed >= st){
int m = st + (ed - st) / 2;
if (arr[m] == target){
printf("Value found at index : %d", m);
} else if (arr[m] < target){
binary_search(arr, m + 1, ed, target);
} else {
binary_search(arr, st, m - 1, target);
}
}
}