#include<stdio.h>
#include<string.h>
#define MAXLEN 100
#define N 10
void seqsearch()
{
int a[N],i,x,y;
char ch;
printf("\n\t\t建立一个整数的顺序表(输入-1结束):\n");
for(i=0;i<MAXLEN;i++)
{
printf("\t\t");
scanf("%d",&a[i]);
getchar();
if(a[i]==-1)
{
y=i;
break;
}
}
printf("\n\t\t需要查找请输入Y/N?");
scanf("%c",&ch);/*getchar();*/
while(ch=='y'||ch=='Y')
{
printf("\n\t\t请输入要查找的数据\n\t\t");
scanf("%d",&x);getchar();
i=y-1;
while(i>=0&&a[i]!=x)
i--;
if(i==-1)
printf("\t\t没有找到\n");
else
printf("\t\t已找到,在第%d的位置上\n",i+1);
printf("\t\t继续查找输入Y,否则输入N:\n\t\t");
scanf("%c",&ch);
}
}
void binsearch()
{
int R[MAXLEN],i,k,low,mid,high,m,nn;
char ch;
printf("\t\t建立递增有序的查找顺序表:\n");
for(i=0;i<MAXLEN;i++)
{
printf("\t\t");
scanf("%d",&R[i]);
getchar();
if(R[i]==-1)
{
nn=i;break;
}
}
printf("\n\t\t查找请输入Y,退出输入N:\n\t\t");
scanf("%c",&ch);getchar();
while(ch=='y'||ch=='Y')
{
printf("\n\t\t请输入要查找的数据:\n\t\t");
scanf("%d",&k);getchar();
low=0;high=nn-1;m=0;
while(low<=high)
{
mid=(low+high)/2;
m++;
if(R[mid]>k) high=mid-1;
else if(R[mid]<k) low=mid+1;
else break;
}
if(low>high)
{
printf("\t\t没有找到\n");
printf("\t\t共进行%d次比较。\n",m);
if(R[mid]<k)
mid++;
printf("\t\t可将此数插入到%d的位置上。\n",mid+1);
}
else
{
printf("\t\t要找的数据%d在第%d的位置上。\n",k,mid+1);
printf("\t\t共进行%d次比较。\n",m);
}
printf("\t\t继续查找输入Y,否则输入N:\n\t\t");
scanf("%c",&ch);getchar();
}
}
void main()
{
int choice;
char ch;
ch='y';
while(ch=='y'||ch=='Y')
{
printf("\n\n\n\n");
printf("\t\t\t查找子系统");
printf("\n\t\t**********************************");
printf("\n\t\t 1--顺序查找");
printf("\n\t\t 2--折半查找");
printf("\n\t\t 0--返回");
printf("\n\t\t**********************************");
printf("\n\t\t请选择菜单(0--3):");
scanf("%d",&choice);
switch(choice)
{
case 1: seqsearch(); break;
case 2: binsearch(); break;
case 0: ch='n'; break;
default:printf("\n\t\t菜单选择错误!请重新输入:");
}
}
}
二叉树子系统实现顺序查找,折中查找程序
164 浏览量
2023-08-03
23:10:43
上传
评论
收藏 155KB RAR 举报
山野码农
- 粉丝: 8
- 资源: 39
最新资源
- 部署yolov8的tensorrt模型支持检测分割姿态估计的C++源码+部署步骤.zip
- 以简单、易用、高性能为目标、开源的时序数据库,支持Linux及Windows, Time Series Database.zip
- python-leetcode面试题解之第198题打家劫舍-题解.zip
- python-leetcode面试题解之第191题位1的个数-题解.zip
- python-leetcode面试题解之第186题反转字符串中的单词II-题解.zip
- 一个基于python的web后端高性能开发框架,下载可用
- python-leetcode面试题解之第179题最大数-题解.zip
- python-leetcode面试题解之第170题两数之和III数据结构设计-题解.zip
- python-leetcode面试题解之第168题Excel表列名称-题解.zip
- python-leetcode面试题解之第167题两数之和II输入有序数组-题解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈