search.h
来自「这是本人精心搜集的关于常用图论算法的一套源码」· C头文件 代码 · 共 21 行
H
21 行
#include "Ordered_list.h"
Error_code sequential_search(Ordered_list& the_list, const int &target, int &position)
{ for( position=0; position<the_list.size( ); position++)
{ Record x;
the_list.retrieve(position, x);
if(x==target) return success;
}
return not_present; // 脱离for,则检索不成功
}
Error_code binary_search(Ordered_list& the_list, const int &target, int &position)
{ int bottom=0, top=the_list.size()-1; // 置区间初值
while(bottom<=top)
{ int mid=(bottom+top)/2;
Record x; the_list.retrieve(mid,x);
if(target==x) // 检索成功
{ position = mid; return success; }
if(target>x) bottom=mid+1; // 修改下界 bottom 值
else top=mid-1; // 修改上界top值
}
return not_present; // 脱离while,则 bottom>top 检索不成功
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?