c语言二分法查找例题
时间: 2023-10-26 10:27:27 浏览: 164
在C语言中,二分法查找是一种高效的查找算法。下面是一个示例程序:
```c
#include <stdio.h>
int binarySearch(int arr[], int left, int right, int key) {
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid == key) {
return mid;
} else if (arr[mid < key) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int main() {
int arr[] = {1, 9, 13, 23, 70, 73, 74, 77, 79, 80, 84, 89, 94, 99, 101};
int n;
printf("请输入一个数字:");
scanf("%d", &n);
int index = binarySearch(arr, 0, sizeof(arr) / sizeof(arr<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [c程序设计习题参考(谭浩强三版)习题参考解答](https://download.csdn.net/download/wmxnzn/2656246)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [C语言经典100题——用二分法在一个有序数组中查找某个数字](https://blog.csdn.net/yahid/article/details/123389973)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [详解【C语言】中的二分查找法和折半查找法(例题解答)](https://blog.csdn.net/m0_63325890/article/details/120993495)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文