数据结构---顺序查找和折半查找

实现顺序查找和折半查找,
对两种查找方法作比较

/****************************************************
  @title: 数据结构实验
  @name: <实验9-1> 顺序查找和折半查找
  @object:
      [实验目的]
          实现顺序查找和折半查找,
          对两种查找方法作比较
      [实验提示]
          1. 编写顺序查找和折半查找算法
          2. 修改算法把查找过程中所作比较及其结果
             打印出来,观察并对比查找过程
          3. 分析并计算两种算法的平均查找长度
  @include:
  @usage:
      请查看"TO-DO列表",根据要求完成代码
  @copyright: BTC 2004, Zhuang Bo
  @author: Zhuang Bo
  @date: 2004
  @description:
*****************************************************/

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

#define ElemType int

//顺序查找
int SqSearch(ElemType a[],int n,ElemType x);
int SqSearch2(ElemType a[],int n,ElemType x); //打印查找过程 
//折半查找
int BinSearch(ElemType a[],int n,ElemType x);
int BinSearch2(ElemType a[],int n,ElemType x); //打印查找过程
//打印数组数据
void PrintArray(ElemType a[],int n);

int main()
{
    int i,x;
    //测试数据
    const int N = 9;
    ElemType a1[N+1]={0,34,23,12,56,90,78,89,45,67};
    ElemType a2[N+1]={0,12,23,34,45,56,67,78,89,90};

    //顺序查找
    printf("\n顺序查找\n");
    printf("a1[]="); 
    PrintArray(a1,N);

    printf("\n输入要查找的数据: ");
    scanf("%d",&x);

    if((i=SqSearch(a1,N,x))>0) //找到 
        printf("\n找到 x==a1[%d]\n",i);
    else //未找到 
        printf("\n找不到 %d \n",x);

    printf("\n查找过程:");
    SqSearch2(a1,N,x);
    printf("\n完成\n");

    //折半查找
    printf("\n折半查找\n");
    printf("a2[]=");
    PrintArray(a2,N);

    printf("\n输入要查找的数据: ");
    scanf("%d",&x);

    if((i=BinSearch(a2,N,x))>0) //找到 
        printf("\n找到 x==a2[%d]\n",i);
    else //未找到
        printf("\n找不到 %d \n",x);

    printf("\n查找过程:");
    BinSearch2(a2,N,x);
    printf("\n完成\n");

    system("pause");
    return 0;
}

//在数组a[1..n]中顺序查找x
//  找到时返回元素下标,否则返回0 
int SqSearch(ElemType a[],int n,ElemType x)
{
    //-------------------------------------
    // TODO (#1#): 顺序查找算法
    a[0]=x;
    for (n;a[n]!=x;--n)
    {
        ;
    }
    //-------------------------------------
    return n; //找不到 
}

int SqSearch2(ElemType a[],int n,ElemType x)
{
    //-------------------------------------
    // TODO (#1#): 顺序查找算法,打印每次比较结果 
    a[0]=x;
    for (n;a[n]!=x;--n)
    {
        printf("与a[%d]=%d比较\n",n,a[n]);
    }
    //-------------------------------------
    printf("与a[%d]=%d比较\n",n,a[n]);
    return n; //找不到 
}

//在数组a[1..n]中折半查找x
//  找到时返回元素下标,否则返回0 
//前提:a[1..n]是非递减有序的 
int BinSearch(ElemType a[],int n,ElemType x)
{
    //-------------------------------------
    // TODO (#1#): 折半查找算法
    int low=1;
    int high=n;
    while (low<=high)
    {
        int mid=(low+high)/2;
        if (a[mid]==x)
        {
            return mid;
        }
        else if (a[mid]<x)
        {
            low=mid+1;
        }
        else high=mid-1;
    }
    //-------------------------------------
    return 0; //找不到 
}

int BinSearch2(ElemType a[],int n,ElemType x)
{
    //-------------------------------------
    // TODO (#1#): 折半查找算法,打印每次比较结果
    int low=1;
    int high=n;
    while (low<=high)
    {
        int mid=(low+high)/2;
        if (a[mid]==x)
        {
            printf("与a[%d]=%d比较\n",mid,a[mid]);
            return mid;
        }
        else if (a[mid]<x)
        {
            printf("与a[%d]=%d比较\n",mid,a[mid]);
            low=mid+1;
        }
        else if (a[mid]>x)
        {
            printf("与a[%d]=%d比较\n",mid,a[mid]);
            high=mid-1;
        } 
    }
    //-------------------------------------
    return 0;
}

//打印数组数据a[1..n]
void PrintArray(int a[],int n)
{
    int i;
    printf("{ ");
    for(i=1; i<=n; i++)
        printf("%d ",a[i]);
    printf("}\n");
}

这里写图片描述

  • 10
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值