【数据结构】第7章 查找 实验11:查找

【折半查找、平衡二叉树的调整B树B+树哈希表


题目:折半查找

一、实验目的和要求

(1)熟悉C++的上机环境,进一步掌握C++的结构特点;
(2)掌握查找。

二、实验环境

Windows10, CodeBlocks.

三、实验内容及实施

【实验内容】
实验11:折半查找
要求:给定一组数序列,输入key, 用折半查找算法查找,并输出查找结果
【源程序】

#include <bits/stdc++.h>
using namespace std;

typedef int KeyType;
typedef int InfoType;
const int N = 100000;
typedef struct 
{
    KeyType key;
    InfoType otherinfo;
}ElemType;
typedef struct 
{
    ElemType R[N];
    int length;
}SSTable;

int Search_Bin(SSTable ST, KeyType key)
{
    int low = 1, high = ST.length;
    while(low <= high)
    {
        int mid = (low + high) / 2;
        if(key == ST.R[mid].key)
            return mid;
        else if(key < ST.R[mid].key)
            high = mid - 1;
        else
            low = mid + 1; 
    }
    return 0;
}

int main()
{
    int key;
    SSTable ST;
    cout<<"请输入数据个数:";
    cin>>ST.length;
    cout<<"请依次输入这"<<ST.length<<"个数据:"<<'\n';
    for(int i = 1; i <= ST.length; i++)
        cin>>ST.R[i].key;
    while(1)
    {
        cout<<"请输入要查找的key值(输入0退出本程序):";
        cin>>key;
        if(key == 0)
        {
            cout<<"感谢使用本程序,祝您生活愉快!"<<'\n';
            break;
        }
        cout<<"查找结果为:";
        if(Search_Bin(ST, key))
            cout<<Search_Bin(ST, key)<<'\n';
        else
            cout<<"此数据不存在!"<<'\n';
    }
    return 0;
}
/*
11
5 16 20 27 30 36 44 55 60 67 71
*/

四、实验结果

在这里插入图片描述

五、实验讨论

(1)熟悉了C++的上机环境,进一步掌握C++的结构特点;
(2)掌握了查找。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_碗碗儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值