几种形式下的二分搜索
来源:互联网 发布:阿里云域名访问 编辑:程序博客网 时间:2024/05/15 06:38
操作对象:
给定一个不减序列 int incre[9]={1,2,2,2,2,4,6,6,8};
给定一个不增序列 int decre[9]={8,6,6,4,2,2,2,2,1};
第一种:int binary_search(int *a,int x,int y,intv); 在[x,y)区间中查找满足a[i]=v的i的值,若不存在,则返回-1;其中a[]为不减序列
int Binary_Search(int *a,int x,int y,int v) //[x,y){ int m; while(x<y){ m=(x+y)/2; if(a[m]==v) return m; else if(a[m]<v) x=m+1; else y=m; } return -1;}
第二种:int binary_search_decre(int *a,int x,int y,int v);在[x,y]区间中查找满足a[i]=v的i的值,若不存在,则返回-1;其中a[]为不增序列int Binary_Search_decre(int *a,int x,int y,int v) //[x,y){ int m; while(x<y){ m=(x+y)/2; if(a[m]==v) return m; else if(a[m]<v) y=m; else x=m+1; } return -1;}
第三种:int lower_bound(int *a,int x,int y,int v);在[x,y)中查找满足a[i]>=v最小的i的值;其中a[i]为单调不减int lower_Bound(int *a,int x,int y,int v) //[x,y){ int m; while(x<y){ m=(x+y)/2; if(a[m]>=v) y=m; else x=m+1; } return x;}测试结果,和C++标准库中lower_bound()用法的结果比较
第三种:lower_bound_decre(int *a,int x,int y,int v);在[x,y)区间中查找a[i]<=v的最小i的值;其中a[]为单调不增int lower_bound_decre(int *a,int x,int y,int v){ int m; while(x<y){ m=(x+y)/2; if(a[m]<=v) y=m; else x=m+1; } return x;}
第四种:int upper_bound(int *a,int x,int y,int v);在[x,y)区间中查找满足a[i]>v的最小的i的值;其中a[]为单调不减int upper_Bound(int *a,int x,int y,int v){ int m; while(x<y){ m=(x+y)/2; if(a[m]<=v) x=m+1; else y=m; } return x;}测试结果以及与C++标准库中upper_bound()的比较
第五种:int upper_bound_decre(int *a,int x,int y,int v);在[x,y)区间中查找a[i]<v的最小的i的值;其中i为单调不增int upper_bound_decre(int *a,int x,int y,int v){ int m; while(x<y){ m=(x+y)/2; if(a[m]>=v) x=m+1; else y=m; } return x;}
完整的代码:
/** author:liuwen*/#include <iostream>#include <cstring>#include <cstdio>#include <string>#include <algorithm>using namespace std;int incre[9]={1,2,2,2,2,4,6,6,8};int decre[9]={8,6,6,4,2,2,2,2,1};int Binary_Search(int *a,int x,int y,int v) //[x,y){ int m; while(x<y){ m=(x+y)/2; if(a[m]==v) return m; else if(a[m]<v) x=m+1; else y=m; } return -1;}int Binary_Search_decre(int *a,int x,int y,int v) //[x,y){ int m; while(x<y){ m=(x+y)/2; if(a[m]==v) return m; else if(a[m]<v) y=m; else x=m+1; } return -1;}int lower_Bound(int *a,int x,int y,int v) //[x,y){ int m; while(x<y){ m=(x+y)/2; if(a[m]>=v) y=m; else x=m+1; } return x;}//[x,y) 在一个单调不增的序列decre[]中,查找decre[]满足decre[i]<=v 条件的最小的iint lower_bound_decre(int *a,int x,int y,int v){ int m; while(x<y){ m=(x+y)/2; if(a[m]<=v) y=m; else x=m+1; } return x;}int upper_Bound(int *a,int x,int y,int v){ int m; while(x<y){ m=(x+y)/2; if(a[m]<=v) x=m+1; else y=m; } return x;}//[x,y) 在一个单调不增的序列decre[]中,查找decre[]满足decre[i]<v 条件的最小的iint upper_bound_decre(int *a,int x,int y,int v){ int m; while(x<y){ m=(x+y)/2; if(a[m]>=v) x=m+1; else y=m; } return x;}int main(){ freopen("in.txt","r",stdin); cout<<"三种二分搜索(binary_search, lower_bound, upper_bound"<<endl; cout<<"不减序列 incre[9]={1,2,2,2,2,4,6,6,8} "<<endl; cout<<"不增序列 decre[9]={8,6,6,4,2,2,2,2,1} "<<endl<<endl; ///**************测试1: binary_search cout<<"**********测试1: binary_search**********"<<endl; cout<<"不减序列 incre[9]={1,2,2,2,2,4,6,6,8} "<<endl; cout<<"Binary_Search(incre,0,7,2) "<<Binary_Search(incre,0,7,2)<<endl; cout<<"Binary_Search(incre,0,7,4) "<<Binary_Search(incre,0,7,4)<<endl; cout<<"Binary_Search(incre,0,7,1) "<<Binary_Search(incre,0,7,1)<<endl; cout<<"Binary_Search(incre,0,7,10) "<<Binary_Search(incre,0,7,10)<<endl; cout<<"不增序列 decre[9]={8,6,6,4,2,2,2,2,1} "<<endl; cout<<"Binary_Search_decre(decre,0,7,2) "<<Binary_Search_decre(decre,0,7,2)<<endl; cout<<"Binary_Search_decre(decre,0,7,4) "<<Binary_Search_decre(decre,0,7,4)<<endl; cout<<"Binary_Search_decre(decre,0,7,1) "<<Binary_Search_decre(decre,0,7,1)<<endl; cout<<"Binary_Search_decre(decre,0,7,10) "<<Binary_Search_decre(decre,0,7,10)<<endl<<endl; ///**************测试2: lower_bound cout<<"**********测试2: lower_bound**********"<<endl; cout<<"不减序列 incre[9]={1,2,2,2,2,4,6,6,8} "<<endl; cout<<"lower_Bound(incre,0,9,2) "<<lower_Bound(incre,0,9,2)<<endl; cout<<"lower_Bound(incre,0,9,6) "<<lower_Bound(incre,0,9,6)<<endl; cout<<"lower_Bound(incre,0,9,4) "<<lower_Bound(incre,0,9,4)<<endl; cout<<"lower_Bound(incre,0,9,5) "<<lower_Bound(incre,0,9,5)<<endl; cout<<"lower_Bound(incre,0,9,8) "<<lower_Bound(incre,0,9,8)<<endl; cout<<"lower_Bound(incre,0,9,10) "<<lower_Bound(incre,0,9,10)<<endl; cout<<"跟C++标准库里面的 lower_bound()比较"<<endl; cout<<"lower_bound(incre,incre+9,2)-incre "<<lower_bound(incre,incre+9,2)-incre<<endl; cout<<"lower_bound(incre,incre+9,6)-incre "<<lower_bound(incre,incre+9,6)-incre<<endl; cout<<"lower_bound(incre,incre+9,4)-incre "<<lower_bound(incre,incre+9,4)-incre<<endl; cout<<"lower_bound(incre,incre+9,5)-incre "<<lower_bound(incre,incre+9,5)-incre<<endl; cout<<"lower_bound(incre,incre+9,8)-incre "<<lower_bound(incre,incre+9,8)-incre<<endl; cout<<"lower_bound(incre,incre+9,10)-incre "<<lower_bound(incre,incre+9,10)-incre<<endl<<endl; ///**************测试2: lower_bound_decre cout<<"测试lower_bound_decre(decre,0,9,v)\n" <<"在一个单调不增的序列decre[]中,查找decre[]满足decre[i]<=v 条件的最小的i"<<endl; cout<<"不增序列 decre[9]={8,6,6,4,2,2,2,2,1} "<<endl; cout<<"lower_bound_decre(decre,0,9,2) "<<lower_bound_decre(decre,0,9,2)<<endl; cout<<"lower_bound_decre(decre,0,9,8) "<<lower_bound_decre(decre,0,9,8)<<endl; cout<<"lower_bound_decre(decre,0,9,6) "<<lower_bound_decre(decre,0,9,6)<<endl; cout<<"lower_bound_decre(decre,0,9,4) "<<lower_bound_decre(decre,0,9,4)<<endl; cout<<"lower_bound_decre(decre,0,9,10) "<<lower_bound_decre(decre,0,9,10)<<endl; cout<<"lower_bound_decre(decre,0,9,0) "<<lower_bound_decre(decre,0,9,0)<<endl; cout<<"lower_bound_decre(decre,0,9,-1) "<<lower_bound_decre(decre,0,9,-1)<<endl<<endl; ///**************测试3: upper_bound cout<<"**********测试3: upper_bound**********"<<endl; cout<<"不减序列 incre[9]={1,2,2,2,2,4,6,6,8} "<<endl; cout<<"upper_Bound(incre,0,9,2) "<<upper_Bound(incre,0,9,2)<<endl; cout<<"upper_Bound(incre,0,9,1) "<<upper_Bound(incre,0,9,1)<<endl; cout<<"upper_Bound(incre,0,9,4) "<<upper_Bound(incre,0,9,4)<<endl; cout<<"upper_Bound(incre,0,9,6) "<<upper_Bound(incre,0,9,6)<<endl; cout<<"upper_Bound(incre,0,9,8) "<<upper_Bound(incre,0,9,8)<<endl; cout<<"upper_Bound(incre,0,9,10) "<<upper_Bound(incre,0,9,10)<<endl; cout<<"与C++标准库中upper_bound()的比较"<<endl; cout<<"upper_bound(incre,incre+9,2)-incre "<<upper_bound(incre,incre+9,2)-incre<<endl; cout<<"upper_bound(incre,incre+9,1)-incre "<<upper_bound(incre,incre+9,1)-incre<<endl; cout<<"upper_bound(incre,incre+9,4)-incre "<<upper_bound(incre,incre+9,4)-incre<<endl; cout<<"upper_bound(incre,incre+9,6)-incre "<<upper_bound(incre,incre+9,6)-incre<<endl; cout<<"upper_bound(incre,incre+9,8)-incre "<<upper_bound(incre,incre+9,8)-incre<<endl; cout<<"upper_bound(incre,incre+9,10)-incre "<<upper_bound(incre,incre+9,10)-incre<<endl<<endl; ///**************测试4: upper_bound_decre cout<<"**********测试4: upper_bound_decre**********\n" <<"在一个单调不增的序列decre[]中,查找decre[]满足decre[i]<v 条件的最小的i"<<endl; cout<<"不增序列 decre[9]={8,6,6,4,2,2,2,2,1} "<<endl; cout<<"upper_bound_decre(decre,0,9,2) "<<upper_bound_decre(decre,0,9,2)<<endl; cout<<"upper_bound_decre(decre,0,9,4) "<<upper_bound_decre(decre,0,9,4)<<endl; cout<<"upper_bound_decre(decre,0,9,6) "<<upper_bound_decre(decre,0,9,6)<<endl; cout<<"upper_bound_decre(decre,0,9,8) "<<upper_bound_decre(decre,0,9,8)<<endl; cout<<"upper_bound_decre(decre,0,9,1) "<<upper_bound_decre(decre,0,9,1)<<endl; cout<<"upper_bound_decre(decre,0,9,10) "<<upper_bound_decre(decre,0,9,10)<<endl; cout<<"upper_bound_decre(decre,0,9,-1) "<<upper_bound_decre(decre,0,9,-1)<<endl; return 0;}
0 0
- 几种形式下的二分搜索
- 二分搜索的几点
- iOS系统下,应用程序运行的几种形式。
- Linux下的几种搜索命令
- Linux下的几种搜索命令
- amd64下的几种地址形式
- DDOS 的几种形式
- 封装的几种形式
- 数据传输的几种形式
- ListView的几种形式
- ListView的几种形式
- pooling的几种形式
- 几种编码的形式的介绍
- 几种编码的形式的介绍
- Singleton模式的几种形式
- JSON的几种调用形式
- C#数据类型转换的几种形式
- Object转为String的几种形式
- 静默年华,许下春暖花开的心愿
- JVM Crash, 求如何分析原因.[已分析,和提出了解决方案]
- 嵌入式 NtpClient 选项用法详解
- shell编程笔记-入门概念
- 【转】深入理解abstract class和interface
- 几种形式下的二分搜索
- poj 3921 Destroying the bus stations(最小割,点连通+floyd)
- 重载函数
- LuCI实现启动应用程序等脚本命令
- 350 - Pseudo-Random Numbers
- jquery代码性能相关的一些小技巧
- 练习2.41-练习2.47
- 单词寻找----CSDN 英雄会
- shell编程笔记-基础