数据结构实验5:查找

实验五:查找算法

实验目的
1.掌握顺序查找技术和拆半查找技术;
2. 掌握查找的算法实现

实验内容
1.产生n个随机整数用顺序查找的方法进行查找操作,要求输出要找关键字Key的过程。
1.给定n个有序整数用折半查找的方法进行查找操作,找关键字Key的过程。


分析
签到级难度,主要是考察折半查找思想的理解和应用。折半查找又称二分查找,需要先对数组
进行排序。为了不打乱原来的数组,可以将排序后的数组另外保存,为了求出查到的数在原来的
数组所在的下标,可以用一个指针数组来排序和查找,得到的指针减去原数组的地址即可得到
所求树的位置。时间复杂度log2(n)。

MyCode

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#include<time.h>
using namespace std;
const int maxn = 20;		//size of randon integer
static int times = 1;		//used by printInt() to control the integer output each line 
int randArray[maxn];		//the array saving randon integer

//controal the format of print integer	
void  printInt(int n){
   		
	string blank = "              ";
	int len = 0;
	int tn = n;
	while (tn /= 10) len++;
	cout << n << blank.substr(0, 8-len)
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值