binary2.cpp

来自「数据结构与程序设计教材源码 数据结构与程序设计教材源码」· C++ 代码 · 共 27 行

CPP
27
字号
 
Error_code binary_search_2(const Ordered_list &the_list,
                           const Key &target, int &position)
/* 
 
Post: If a Record in the_list   has key equal
      to target, then position locates
      one such entry and a code of success is returned.
      Otherwise, not_present is returned and position is
      undefined.
Uses: Methods for classes Ordered_list and Record. 
 
*/

{
   Record data;
   int bottom = 0, top = the_list.size() - 1;
   while (bottom <= top) {
      position = (bottom + top) / 2;
      the_list.retrieve(position, data);
      if (data == target) return success;
      if (data < target) bottom = position + 1;
      else top = position - 1;
   }
   return not_present;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?