正确的二分法数据查找算法——百度百科里的那个算法是错误的
2013-07-08 16:22
218 查看
正确的二分法数据查找算法,百度百科里有一个算法,很多人都在研究,但是我跑过之后结果是错的,有问题,此算法经本人验证无误,贴出来与大家共享。
//dichotomy algorithm #include <iostream> using namespace std; const int NUM = 16; const int Found(const int a[], const int obj, const intlen); int main() { int a[NUM]={-1,11,23,56,98,129,189,452,654,789,1023,2892,5022,6344,7680,9988}; int isFound = NUM; for (int i = 0; i < NUM; i++) { cout << "a["<< i <<"]=" << a[i]<< "\t"; if ((i+1)%3 == 0) cout<< '\n'; } cout<< endl; isFound =Found(a,56,NUM); if (isFound == NUM) cout << "Data not found.\n"; else cout << "Data is a[" << isFound << "]" << endl; return 0; } const int Found(const int a[], const int obj, const intlen) { int front = 0, end =len-1, i=(front+end)/2; while ( front < end ) { if (a[i] == obj) returni; else if (a[i] < obj) { front = i; } else { end = i; } i = (end+front)/2; } return len; }
相关文章推荐
- Java学习之数组1(1.数组的声明;2.元素为引用数据类型的数组;3.关于main方法里的String[] args;4.数组排序;5.数3退1 数组算法,(用数组模拟链表);6数组查找之二分法;7数组的拷贝)
- 数据结构——算法之(019)( 10分钟写出二分法查找,并调试通过)
- [php] 数据结构&算法(PHP描述) 查找&&二分法查找
- 数据结构——算法之(005)(输入一个已经按升序排序过的数组和一个数字, 在数组中查找两个数,使得它们的和正好是输入的那个数字)
- 数据查找算法---顺序查找、二分法查找、二叉树查找、hash查找
- 二分法从有序数列中查找数据
- I学霸官方免费教程二十九:Java查找算法之二分法查找
- 实现了一个压缩算法,在数据高度压缩的前提下,还可以快速查找 key
- 数据结构算法学习-1. 查找(Search)概论与三种顺序查找算法
- python的算法:二分法查找(1)
- 数据结构与算法学习笔记之写链表代码的正确姿势(下)
- Java数据结构与算法之【二分查找】
- 白话经典算法系列之四 直接选择排序及交换二个数据的正确实现
- 关于数据类型—长期以来苦苦查找错误的思考
- 二分法查找数据
- [算法][二分法查找]
- java 数据结构与算法 之查找法
- pta-数据结构与算法题目集(中文)--4-4 链式表的按序号查找
- 数据结构与算法以及刷题leetocde 007 查找表 set和map相关
- 算法-二分法查找