实验5 数组第二小题题解

二分查找法,严禁抄袭。

   1:  /*
   2:   * 作    者:邓勖帆 (Kyle)
   3:   * 个人主页:U2U (Http://U2USoft.CNBlogs.com/)
   4:   * 
   5:   * 项    目:实验5.2  数组
   6:   * 文件名称:Assignment_52.cpp
   7:   * 创建日期:2009年6月15日
   8:   * 当前版本:1.1
   9:   * 摘    要:二分查找法
  10:   * 
  11:   * 编码风格:C/C# Crossover
  12:   * 目标平台:Windows 2003/XP以上(x86/x64)
  13:   * 编译说明:在VC++ 2008、Intel C++ Compiler 11.0下正确编译
  14:   */
  15:   
  16:  #include "stdafx.h"
  17:  #include <conio.h>
  18:   
  19:  #define pause() do{printf("\n请按任意键继续...\n");_getch();} while(0)
  20:  #define BSEARCH_ARY(ary,key,func) myBSearch(ary,key,sizeof(ary)/sizeof(ary[0]),sizeof(ary[0]),func)
  21:   
  22:  //----------------------------------------------------------
  23:  // 函数声明
  24:  int myBSearch
  25:  (
  26:   const void* _Base,
  27:   const void* _Key,
  28:   int _NumOfElements,
  29:   size_t _SizeOfElement,
  30:   int _ptFuncCompare(const void*, const void*)
  31:  );
  32:   
  33:  int cmp(const void* _elementA,const void* _elementB);
  34:  //----------------------------------------------------------
  35:   
  36:  int main()
  37:  {
  38:      int k[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  39:      int p = 16;
  40:      printf("Index At: %d\n",BSEARCH_ARY(k, &p, cmp));
  41:   
  42:      _getch();
  43:      return 0;
  44:  }
  45:  //-----------------------------------------------------------
  46:  // 函数名称:
  47:  //     myBSearch      算是非常简短的二分搜索
  48:  // 参数:
  49:  //    - const void* _Base           数据基址
  50:  //    - const void* _Key            查找目标
  51:  //    - int _NumOfElements          元素数量
  52:  //    - size_t _SizeOfElement       元素大小
  53:  //    - int _PtFuncCompare(const void*, const void*)  比较函数
  54:  // 返回:
  55:  //      int     以0为起始的元素索引,如果没找到元素则返回-1
  56:  // 说明:
  57:  //     
  58:  //-----------------------------------------------------------
  59:  int myBSearch
  60:  (
  61:   const void* _Base,
  62:   const void* _Key,
  63:   int _NumOfElements,
  64:   size_t _SizeOfElement,
  65:   int _PtFuncCompare(const void*, const void*)
  66:   ){
  67:      const char* base = (const char*) _Base;
  68:   
  69:      for (size_t lim = _NumOfElements; lim != 0; lim >>= 1)
  70:      {
  71:          const char* p = base + (lim >> 1) * _SizeOfElement;
  72:          int cmp = _PtFuncCompare(_Key, p);
  73:   
  74:          if (cmp == 0)
  75:              return ((p - (const char*)_Base) / _SizeOfElement);
  76:          if (cmp > 0)
  77:          {
  78:              base = (const char*)p + _SizeOfElement;
  79:              lim--;
  80:          }
  81:      }
  82:   
  83:      return (-1);
  84:  }
  85:   
  86:  //----------------------------------------------------------
  87:  // 实现一个比较函数
  88:  int cmp(const void* _elementA, const void* _elementB)
  89:  {
  90:      return (*(int*)_elementA - *(int*)_elementB);
  91:  }
  92:   
  93:  //----------------------------------------------------------
posted @ 2009-06-18 07:06 U2U 阅读(197) 评论(0) 编辑 收藏