您的位置:首页 > 其它

常见查找算法之—二分查找

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: