二分搜索算法
来源:互联网 发布:mac os x leopard下载 编辑:程序博客网 时间:2024/04/30 06:35
二分搜索算法是计算机程序设计中的基础算法,1946年第一篇二分搜索算法的论文发表,第一个正确的算法实现是在1962年,中间相隔16年,这一事实令人深思。据了解训练有素的程序员仅有10%的人能够在数小时内写出完全正确的代码实现。因此,我也进行了尝试。现将实现如下贴出,不能保证完全正确,欢迎批评指正。
BinarySearch.h
#ifndef BinarySearch_H#define BinarySearch_H#include <assert.h>template<class T>T *binarySearch(T value,T a[],int left,int right,int *index){ assert(a); if (left > right) { *index = -1; return NULL; } int mid = (right + left)/2; if (value == a[mid]) { *index = mid; return &a[mid]; } else if (value < a[mid]) { return binarySearch(value,a,left,mid-1,index); } else if (value > a[mid]) { return binarySearch(value,a,mid+1,right,index); }}template<class T>T *binSearch(T value,T a[],int n,int *index){ assert(a); int left = 0; int right = n-1; int mid; while (left <= right) { mid = (left+right)/2; if (a[mid] == value) { *index = mid; return &a[mid]; } else if (a[mid] > value) { right = mid - 1; } else { left = mid + 1; } } *index = -1; return NULL;}#endif
main.cc
using namespace std;template <class T>void output(T a[],int n);int main(){ int a[] = {-5,-3,0,0,2,2,2,2,6,8}; int n = sizeof(a)/sizeof(a[0]); cout << "The a[] "; output(a,n); int index; int test[] = {-6,-5,-3,0,0,1,2,2,2,2,6,8,9}; int len = sizeof(test)/sizeof(test[0]); int i; cout << "The test[] "; output(test,len); cout << "递归二分搜索算法" <<endl; for (i = 0;i < len;i++) { if (binarySearch(test[i],a,0,n-1,&index)==NULL) { cout << test[i] << " The index is null" << endl; continue; } cout << *binarySearch(test[i],a,0,len-1,&index) << " "; cout <<"The index is:"<< index << endl; } cout << "循环二分搜索算法" <<endl; for (i = 0;i < len;i++) { if (binSearch(test[i],a,n,&index)==NULL) { cout << test[i] << " The index is null" << endl; continue; } cout << *binSearch(test[i],a,len,&index) << " "; cout <<"The index is:"<< index << endl; } return 0;}template <class T>void output(T a[],int n){ assert(a); int i = 0; for (; i < n; i++) cout << a[i] <<" "; cout << endl;}
makefile
main:main.o g++ -o main main.omain.o:main.cc BinarySearch.h g++ -c main.ccclean: rm -f main *.o *.s *.i *.gch执行 make命令,./main结果
root@duyuqi-OptiPlex-380:~/DataStruct/Algorithms/search# makeg++ -c main.ccg++ -o main main.oroot@duyuqi-OptiPlex-380:~/DataStruct/Algorithms/search# ./mainThe a[] -5 -3 0 0 2 2 2 2 6 8 The test[] -6 -5 -3 0 0 1 2 2 2 2 6 8 9 递归二分搜索算法-6 The index is null-5 The index is:0-3 The index is:10 The index is:20 The index is:21 The index is null2 The index is:62 The index is:62 The index is:62 The index is:66 The index is:88 The index is:99 The index is null循环二分搜索算法-6 The index is null-5 The index is:0-3 The index is:10 The index is:20 The index is:21 The index is null2 The index is:62 The index is:62 The index is:62 The index is:66 The index is:88 The index is:99 The index is null
- 【基础算法】搜索-二分搜索
- C#二分搜索算法
- 二分搜索算法
- 二分搜索算法
- 二分搜索算法
- 二分搜索算法
- 二分搜索算法
- 简单算法--二分搜索
- 二分搜索算法细节
- 研究二分搜索算法
- 二分搜索算法
- 二分搜索算法
- 二分搜索算法
- 二分搜索算法
- 二分搜索算法
- 二分搜索算法
- 分治算法--二分搜索
- java二分搜索算法
- 目录扫描printfdir
- 低调做人,中调做事
- 在uboot中添加新的命令
- 在freescale平台下,添加一个GPIO驱动(power key)
- fastboot命令驱动修改(uboot-imx/drivers/fastboot/fastboot.c)
- 二分搜索算法
- [usaco] Cow Pedigrees
- HDU 1022 Train Problem I
- error C2011: '_INTERFACE_INFO' : 'struct' type redefinition
- 机房收费系统文档总结
- A Simple Problem with Integers
- uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型 - 大总结,看完全明白了
- 积分器与低通滤波
- MATLAB mex -setup 使用