rbinary2.cpp

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

CPP
41
字号
 

Error_code recursive_binary_2(const Ordered_list &the_list, const Key &target,
                       int bottom, int top, int &position)
/* 
 
Pre:  The indices bottom to top define the
     range in the list to search for the target.
Post: If a Record in the range
      from bottom to top 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: recursive_binary_2, together with methods from the classes
      Ordered_list and Record.
 
*/
{
   Record data;
   if (bottom <= top) {
      int mid = (bottom + top) / 2;
      the_list.retrieve(mid, data);
      if (data == target) {
         position = mid;
         return success;
      }

      else if (data < target)
         return recursive_binary_2(the_list, target, mid + 1, top, position);
      else
         return recursive_binary_2(the_list, target, bottom, mid - 1, position);
   }
   else return not_present;
}
 
Error_code run_recursive_binary_2(const Ordered_list &the_list,
                               const Key &target, int &position)
{
   return recursive_binary_2(the_list, target, 0, the_list.size() - 1, position);
}

⌨️ 快捷键说明

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