- 1049
- 积分
- 90
- 粉丝
- 141
- 获赞
- 5
- 评论
- 137
- 收藏
分类专栏
win10 chm文件打开空白怎么办 win10 chm文件打开空白解决办法
珈丞: 谢谢清汤大老爷和天杀的win10
CSDN-Ada助手: 推荐 Python入门 技能树:https://edu.csdn.net/skill/python?utm_source=AI_act_python
weiabc: CTRL+ PAUSE BREAK 退出程序
weiabc: [code=objc] //二分查找法,前提是元素已经有序,递归版本 int BinarySearchR(int arr[], int n, int val, int left, int right) { int mid = (left + right) / 2; //这一句要在循环内,不然循环的条件(即查找的范围)得不到收敛 if (arr[mid] < val) //待查找值在右侧 { left = mid + 1; //待查数组左边界进行修正 BinarySearchR(arr, n, val, left, right); } else if (arr[mid] == val) //待查找值已经找到 { return mid; //返回索引 } else //待查找值在左侧 { right = mid - 1; BinarySearchR(arr, n, val, left, right); } } [/code]
weiabc: [code=csharp] #include <stdio.h> //线性查找 int LinearSearch(int arr[], int n, int val) { for (int i = 0; i < n; i++) { if (arr[i] == val) { return i; } } return -1; } //二分查找法,前提是元素已经有序 int BinarySearch(int arr[], int n, int val) { int left = 0, right = n - 1; while (left <= right) { int mid = (left + right) / 2; //这一句要在循环内,不然循环的条件(即查找的范围)得不到收敛 if (arr[mid] < val) //待查找值在右侧 { left = mid + 1; //待查数组左边界进行修正 } else if (arr[mid] == val) //待查找值已经找到 { return mid; //返回索引 } else //待查找值在左侧 { right = mid - 1; } } } int main(int argc, char const *argv[]) { int arr[] = { 1,2,3,4,5,10,12,16,19 }; int n = 9; //printf("%d", LinearSearch(arr, n, 5)); printf("%d", BinarySearch(arr, n, 16)); return 0; } [/code]