int main() { //二分法查找算法: 查找(有序排列)数组中6对应的下标并输出 int arr[] = { 1,2,3,4,5,6,7,8,9,10 }; //下标取中(第一个下标为0),进行比较判断 int ret = sizeof(arr) / sizeof(arr[0]); int left = 0; int right = ret-1; while (left <= right) //进行二分查找循环的条件:while(左<右) { int mid = (left + right) / 2; if (6 > arr[mid]) { left = mid + 1; } else if (6 < arr[mid]) { right = mid - 1; } else { printf("找到了,下标为:%d ", mid); break; } } if (left > right) { printf("找不到"); } return 0; }
标签:arr,下标,int,mid,二分法,-----,left From: https://www.cnblogs.com/Hangli123/p/16953348.html