#include<stdio.h>
int main(){
char arr[20] = { 1,2,3,4,5,6,7,8,9,10 };
int left = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
int right = sz - 1;
int k = 9;
while(left <=right) {
int mid = (left + right) / 2;
if (arr[mid] > k) {
right = mid - 1;
}
else if(arr[mid] < k) {
left = mid + 1;
}
else {
printf("找到了,下标是:%d\n", mid);
break;
}
}
if (left > right) {
printf("没有找到\n");
}
return 0;
}
二分查找(在整形有序的数组中查找具体的某个数)
于 2023-04-13 15:10:42 首次发布