• 领导讲话
  • 自我介绍
  • 党会党课
  • 文秘知识
  • 转正申请
  • 问题清单
  • 动员大会
  • 年终总结
  • 工作总结
  • 思想汇报
  • 实践报告
  • 工作汇报
  • 心得体会
  • 研讨交流
  • 述职报告
  • 工作方案
  • 政府报告
  • 调研报告
  • 自查报告
  • 实验报告
  • 计划规划
  • 申报材料
  • 当前位置: 勤学考试网 > 公文文档 > 年终总结 > 正文

    数据结构实验报告---折半查找

    时间:2020-09-01 16:12:01 来源:勤学考试网 本文已影响 勤学考试网手机站

    折半查找:

    代码:

    #include<stdio.h>

    #include<malloc.h>

    typedef struct{

    int *elem;

    int length;

    }sstable;

    int searchbin(sstable ST,int key)

    {

    int low,high,mid;

    low=1;high=ST.length;

    while(low<=high)

    {

    mid=(low+high)/2;

    if(key==ST.elem[mid]) return(mid);

    else if(key<ST.elem[mid])

    high=mid-1;

    else low=mid+1;

    }

    return(0);

    }

    void main()

    {

    int i,key,location;

    sstable ST;

    ST.elem=(int*)malloc(ST.length*sizeof(int));

    printf("请输入表的长度:\n");

    scanf("%d",&ST.length);

    printf("请输入待查找表中的元素:\n");

    for(i=1;i<=ST.length;i++)

    scanf("%d",&ST.elem[i]);

    printf("请输入要查找的元素:\n");

    scanf("%d",&key);

    location=searchbin(ST,key);

    printf("要查找的元素的位置为:%d\n",location);

    }

    运行结果:

    见下页。

    相关热词搜索: 实验报告 折半 数据结构 查找

    • 考试时间
    • 范文大全
    • 作文大全
    • 课程
    • 试题
    • 招聘
    • 文档大全

    推荐访问