折半查找(分治法)

Binary search algorithm

折半查找也称为二分查找,是一个经典的查找算法。

原始数据使用随机函数生成。

采用结构化程序设计,可以很容易改为从标准输入或文件读入数据,只需要修改函数getData即可。

数据个数由宏定义给出,也可以轻松地改为输入。

使用快速排序算法对原始数据进行排序。

这里给出递归和非递归两种过程实现折半查找,非递归的折半查找为正解。

/*
 * 折半查找算法程序(测试数据使用快速算法进行排序)
 * 这里包括递归算法和非递归算法
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "quicksort.h"

#define N 7

void getData(int [], int);
void outputData(int [], int);
int binsearch1(int a[], int low, int high, int val);
int binsearch2(int a[], int low, int high, int val);

int main(void)
{
  int a[N], i;

  getData(a, N); /* 获得数据放入数组a中 */

  printf("Unordered datas: ");
  outputData(a, N);

  quicksort(a, 0, N - 1);

  printf("In sorted order: ");
  outputData(a, N);

  int index, val;
  scanf("%d", &val);
  index = binsearch1(a, 0, N-1, val);
  if(index == -1)
    printf("There is no %d in array A.\n", val);
  else
    printf("%d is at the %dth unit is array A.\n", val, index);

  index = binsearch2(a, 0, N-1, val);
  if(index == -1)
    printf("There is no %d in array A.\n", val);
  else
    printf("%d is at the %dth unit is array A.\n", val, index);

  return 0;
}

int binsearch1(int a[], int low, int high, int val)
{
    int mid;
    if(low > high)
        return -1;
    else
    {
        mid = (low + high) / 2;
        if(a[mid] == val)
            return mid;
        else if(val > a[mid])
            return binsearch1(a, mid+1, high, val);
        else /* if(val < a[mid]) */
            return binsearch1(a, low, mid-1, val);
    }
}

int binsearch2(int a[], int low, int high, int val)
{
    int mid;

    for(;;)
    {
        if(low > high)
            return -1;
        else
        {
            mid = (low + high) / 2;
            if(a[mid] == val)
                return mid;
            else if(val > a[mid])
                low = mid+1;
            else /* if(val < a[mid]) */
                high = mid-1;
        }
    }
}

void getData(int d[], int n)
{
    time_t t;
    srand((unsigned) time(&t));  /* 设置随机数起始值 */

    int i;
    for(i=0; i < n; i++)
        d[i] = rand() % 1000; /* 获得0-999之间的整数值 */
}

void outputData(int d[], int n)
{
    int i;
    for (i = 0; i < n; i++)
      printf("%d ", d[i]);
    printf("\n");
}

关键代码:

// 非递归折半查找算法程序
int binsearch2(int a[], int low, int high, int val)
{
    int mid;

    for(;;)
    {
        if(low > high)
            return -1;
        else
        {
            mid = (low + high) / 2;
            if(a[mid] == val)
                return mid;
            else if(val > a[mid])
                low = mid+1;
            else /* if(val < a[mid]) */
                high = mid-1;
        }
    }
}

快速排序程序头文件quicksort.h:

void quicksort(int a[], int low, int high);
int split(int a[], int low, int high);

快速排序程序quicksort.c:

#include "quicksort.h"

void quicksort(int a[], int low, int high)
{
  int middle;

  if (low >= high)
      return;
  middle = split(a, low, high);
  quicksort(a, low, middle - 1);
  quicksort(a, middle + 1, high);
}

int split(int a[], int low, int high)
{
  int part_element = a[low];

  for (;;) {
    while (low < high && part_element <= a[high])
      high--;
    if (low >= high) break;
    a[low++] = a[high];

    while (low < high && a[low] <= part_element)
      low++;
    if (low >= high) break;
    a[high--] = a[low];
  }

  a[high] = part_element;

  return high;
}


你可能感兴趣的:(二分查找,折半查找)