bisearch.c

来自「掌握如何用C来实现各种算法」· C语言 代码 · 共 85 行

C
85
字号
/*****************************************************************************
*                                                                            *
*  ------------------------------ bisearch.c ------------------------------  *
*                                                                            *
*****************************************************************************/

#include <stdlib.h>
#include <string.h>

#include "search.h"

/*****************************************************************************
*                                                                            *
*  ------------------------------- bisearch -------------------------------  *
*                                                                            *
*****************************************************************************/

int bisearch(void *sorted, const void *target, int size, int esize, int
   (*compare)(const void *key1, const void *key2)) {

int                left,
                   middle,
                   right;

/*****************************************************************************
*                                                                            *
*  Continue searching until the left and right indices cross.                *
*                                                                            *
*****************************************************************************/

left = 0;
right = size - 1;

while (left <= right) {

   middle = (left + right) / 2;

   switch (compare(((char *)sorted + (esize * middle)), target)) {

      case -1:

      /***********************************************************************
      *                                                                      *
      *  Prepare to search to the right of the middle index.                 *
      *                                                                      *
      ***********************************************************************/

      left = middle + 1;
      break;

      case 1:

      /***********************************************************************
      *                                                                      *
      *  Prepare to search to the left of the middle index.                  *
      *                                                                      *
      ***********************************************************************/

      right = middle - 1;
      break;

      case 0:

      /***********************************************************************
      *                                                                      *
      *  Return the exact index where the data has been found.               *
      *                                                                      *
      ***********************************************************************/

      return middle;

   }

}

/*****************************************************************************
*                                                                            *
*  Return that the data was not found.                                       *
*                                                                            *
*****************************************************************************/

return -1;

}

⌨️ 快捷键说明

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