-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbai3.c
34 lines (33 loc) · 783 Bytes
/
bai3.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
#include <stdio.h>
int binarySearch(int arr[], int n, int target) {
int left=0, right=n-1, mid;
while(left<=right) {
mid=left+(right-left)/2;
if (arr[mid]==target)
return mid;
else if (arr[mid]<target)
left=mid+1;
else
right=mid-1;
}
return 0;
}
int main() {
int n, target;
printf("nhap so phan tu cua mang:");
scanf("%d", &n);
int arr[n];
printf("Nhap cac phan tu cua mang: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Nhap so can tim:");
scanf("%d", &target);
int result = binarySearch(arr, n, target);
if (result != 0) {
printf("%d", result);
} else {
printf("khong tim thay ");
}
return 0;
}