常见查找算法之—二分查找
2015-06-04 10:31
197 查看
一,二分查找
二分查找也称拆半查找,算法做为时间复杂度为o(log(n)的算法,也属于在项目中经常会被用到的算法之一
基本步骤:
1,待查找元素与有序表中间元素比较。
2,比中间元素大,修改左边界为中间位置加1,执行第1步。
3,比中间元素小,修改右边界为中间位置减1,执行第1步。
二,源代码
/*************************************************************************
> File Name: binsearch.cpp
> Author:zhangtx
> Mail: 18510665908@163.com
> Created Time: 2015年06月04日 星期四 09时24分19秒
************************************************************************/
#include<iostream>
using namespace std;
template<typename T>
int binSearchRec(int left,int right,T item,T data[])
{
if (right<left)
return -1;
int mid=(left+right)/2;
if (data[mid]==item)
return mid;
else
if (data[mid]<item)
return binSearchRec(mid+1,right,item,data);
else
if (data[mid]>item)
return binSearchRec(left,mid-1,item,data);
}
template<typename T>
int binSearch(int left,int right,T item,T data[])
{
if (right<left)
return -1;
int mid=-1;
while(left<=right)
{
mid=(left+right)/2;
if (data[mid]<item)
left=mid+1;
else
if (data[mid]>item)
right=mid-1;
else
break;
}
if(left>right)
mid=-1;
return mid;
}
int main(int argc,char *argv[])
{
cout<<endl<<"*************************Normal*************************"<<endl;
int testData[]={1,3,5,7,9,12,34,46,76,98,111,222,333,444,555,666};
int pos=binSearch<int>(0,15,11,testData);
cout<<"test not exist,pos should be -1,the result is "<<pos<<endl;
pos=binSearch<int>(0,15,12,testData);
cout<<"test exist,pos should larger than 0,the result is "<<pos<<endl;
pos=binSearch<int>(0,15,1,testData);
cout<<"test left edge,pos should be 0,the result is "<<pos<<endl;
pos=binSearch<int>(0,15,666,testData);
cout<<"test right edge,pos should be 15,the result is "<<pos<<endl;
int testData1[]={1};
pos=binSearch<int>(0,1,1,testData1);
cout<<"test single items,pos should be 0,the result is "<<pos<<endl;
cout<<endl<<"*************************Recu*************************"<<endl;
pos=binSearchRec<int>(0,15,11,testData);
cout<<"test not exist,pos should be -1,the result is "<<pos<<endl;
pos=binSearchRec<int>(0,15,12,testData);
cout<<"test exist,pos should larger than 0,the result is "<<pos<<endl;
pos=binSearchRec<int>(0,15,1,testData);
cout<<"test left edge,pos should be 0,the result is "<<pos<<endl;
pos=binSearchRec<int>(0,15,666,testData);
cout<<"test right edge,pos should be 15,the result is "<<pos<<endl;
pos=binSearchRec<int>(0,1,1,testData1);
return 0;
}
下面是基于C++实际的源码。
三,执行结果
[root@M-192-168-10-225 algo]# g++ binsearch.cpp
[root@M-192-168-10-225 algo]# ./a.out
*************************Normal*************************
test not exist,pos should be -1,the result is -1
test exist,pos should larger than 0,the result is 5
test left edge,pos should be 0,the result is 0
test right edge,pos should be 15,the result is 15
test single items,pos should be 0,the result is 0
*************************Recu*************************
test not exist,pos should be -1,the result is -1
test exist,pos should larger than 0,the result is 5
test left edge,pos should be 0,the result is 0
test right edge,pos should be 15,the result is 15
二分查找也称拆半查找,算法做为时间复杂度为o(log(n)的算法,也属于在项目中经常会被用到的算法之一
基本步骤:
1,待查找元素与有序表中间元素比较。
2,比中间元素大,修改左边界为中间位置加1,执行第1步。
3,比中间元素小,修改右边界为中间位置减1,执行第1步。
二,源代码
/*************************************************************************
> File Name: binsearch.cpp
> Author:zhangtx
> Mail: 18510665908@163.com
> Created Time: 2015年06月04日 星期四 09时24分19秒
************************************************************************/
#include<iostream>
using namespace std;
template<typename T>
int binSearchRec(int left,int right,T item,T data[])
{
if (right<left)
return -1;
int mid=(left+right)/2;
if (data[mid]==item)
return mid;
else
if (data[mid]<item)
return binSearchRec(mid+1,right,item,data);
else
if (data[mid]>item)
return binSearchRec(left,mid-1,item,data);
}
template<typename T>
int binSearch(int left,int right,T item,T data[])
{
if (right<left)
return -1;
int mid=-1;
while(left<=right)
{
mid=(left+right)/2;
if (data[mid]<item)
left=mid+1;
else
if (data[mid]>item)
right=mid-1;
else
break;
}
if(left>right)
mid=-1;
return mid;
}
int main(int argc,char *argv[])
{
cout<<endl<<"*************************Normal*************************"<<endl;
int testData[]={1,3,5,7,9,12,34,46,76,98,111,222,333,444,555,666};
int pos=binSearch<int>(0,15,11,testData);
cout<<"test not exist,pos should be -1,the result is "<<pos<<endl;
pos=binSearch<int>(0,15,12,testData);
cout<<"test exist,pos should larger than 0,the result is "<<pos<<endl;
pos=binSearch<int>(0,15,1,testData);
cout<<"test left edge,pos should be 0,the result is "<<pos<<endl;
pos=binSearch<int>(0,15,666,testData);
cout<<"test right edge,pos should be 15,the result is "<<pos<<endl;
int testData1[]={1};
pos=binSearch<int>(0,1,1,testData1);
cout<<"test single items,pos should be 0,the result is "<<pos<<endl;
cout<<endl<<"*************************Recu*************************"<<endl;
pos=binSearchRec<int>(0,15,11,testData);
cout<<"test not exist,pos should be -1,the result is "<<pos<<endl;
pos=binSearchRec<int>(0,15,12,testData);
cout<<"test exist,pos should larger than 0,the result is "<<pos<<endl;
pos=binSearchRec<int>(0,15,1,testData);
cout<<"test left edge,pos should be 0,the result is "<<pos<<endl;
pos=binSearchRec<int>(0,15,666,testData);
cout<<"test right edge,pos should be 15,the result is "<<pos<<endl;
pos=binSearchRec<int>(0,1,1,testData1);
return 0;
}
下面是基于C++实际的源码。
三,执行结果
[root@M-192-168-10-225 algo]# g++ binsearch.cpp
[root@M-192-168-10-225 algo]# ./a.out
*************************Normal*************************
test not exist,pos should be -1,the result is -1
test exist,pos should larger than 0,the result is 5
test left edge,pos should be 0,the result is 0
test right edge,pos should be 15,the result is 15
test single items,pos should be 0,the result is 0
*************************Recu*************************
test not exist,pos should be -1,the result is -1
test exist,pos should larger than 0,the result is 5
test left edge,pos should be 0,the result is 0
test right edge,pos should be 15,the result is 15
相关文章推荐
- 如何在Bash Shell脚本中显示对话框
- IOS开发之MPMusicPlayerController
- 波克城市棋牌源码,傲翼棋牌源码3D人物效果!(本人二次开发)
- 二叉树的遍历(1):前序遍历
- 内联汇编使用简介
- ca-bundle.crt to java truststore(e.g. trustStore.jks)
- mac os 利用ssh 搭建git server服务器详细教程,以及git基本用法
- java socket 通信实例
- MySQL高可用架构之MHA
- linux定时任务crontab详解
- 二维数组中的查找
- powershell 枚举远程主机登录会话 [PowerQuinsta]
- POJ3262 Protecting the Flowers(贪心)
- 黄聪:wordpress如何添加自定义文章快速编辑按钮
- Error: Cannot run program "/home/xxx/android_developer_tools/android-ndk-r8/ndk-build.cmd": Unknown reason
- PHP几种抓取网络数据的常见方法
- WinForm程序开发
- Android自己主动化測试——CTS測试
- edmx
- 关于iWebOffice中使用变量插入到Office书签里面