折半查找
来源:互联网 发布:c语言成员变量 编辑:程序博客网 时间:2024/06/06 10:50
折半查找
#include <bits/stdc++.h>using namespace std;int BinarySerch1 (int * A, const int x, const int n) { int l = 0; int r = n - 1; while (l <= r) { int m = (l + r) / 2; if (x < A[m]) r = m - 1; else if (x > A[m]) l = m + 1; else return m;//找到x,返回下标m } return -1;//没有找到返回-1}//递归版本int BinarySerch2 (int *A, const int x, int l, int r) { if (l <= r) { int m = (l + r) / 2; if (x < A[m]) BinarySerch2 (A, x, l, m - 1); else if (x > A[m]) BinarySerch2 (A, x, m + 1, r); else return m; } return -1;}int main() { int A[] = {14, 62, 73, 52, 66, 74, 45, 34, 87, 29}; int len = sizeof (A) / sizeof (A[0]); sort (A, A + len);//sort函数升序排列 cout << BinarySerch1 (A, 62, len) << endl; cout << BinarySerch2 (A, 62, 0, len) << endl;}
阅读全文
0 0
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 机房收费系统-报表打印
- 【剑指offer】题61:二叉树序列化、反序列化
- 目前大数据在金融行业的应用主要体现在哪些方面?
- spring boot 打jar包
- 关于泛型介于返回值同static之间的意义
- 折半查找
- # hadoop入门第六篇-Hive实例
- kuangbin 数论 E题
- [Leetcode] 384. Shuffle an Array 解题报告
- mutex&chan
- 【POJ】3180
- 排序之交换排序:冒泡排序and快速排序
- 二进制到八进制的转换
- AVFoundation学习笔记(二):视频播放相关