2008秋-计算机软件基础- 第四章- 分块查找,索引顺序查找
//分块查找,索引顺序查找
#include<stdio.h>
//定义顺序存储线性表的结点结构
struct student
{
long Xuehao;//关键字
int Jihao; //非关键字
};
struct indexNode
{
int address;
int maxKey;
};
int SearchPosition(struct indexNode index[],int length, int someXuehao)
{
int i;
if(someXuehao<=index[0].maxKey)
return index[0].address;
if(someXuehao>=index[length-1].maxKey)
return index[length-1].address;
for(i=0;i<length-1;i++)
{
if(someXuehao>index[i].maxKey && someXuehao<=index[i+1].maxKey)
return index[i+1].address;
}
return -1;
}
int SequencSearch(struct student students[],int startIndex,int length, int someXuehao)
{
//顺序查找,在students[]中查找someXuehao,表长length
//成功返回位置(0~length-1),失败返回-1。
int i;
for(i=startIndex;i<startIndex+length;i++)
{
if(students[i].Xuehao==someXuehao)
return i;
}
return -1;
}
void main()
{
struct student s[15]={1,11,2,22,3,33,4,44,5,55,
6,66,7,77,8,88,9,99,10,1000,
11,110,12,120,13,130,14,140,15,150};
struct indexNode index[3]={0,5,5,10,10,15};
int insertBlockIndex, searchPosition;
insertBlockIndex=SearchPosition(index,3,16);
printf("\ninsertBlockIndex:%d",insertBlockIndex);
searchPosition=SequencSearch(s,insertBlockIndex,5,16);
printf("\n下标为:%d\n",searchPosition);
}
#include<stdio.h>
//定义顺序存储线性表的结点结构
struct student
{
long Xuehao;//关键字
int Jihao; //非关键字
};
struct indexNode
{
int address;
int maxKey;
};
int SearchPosition(struct indexNode index[],int length, int someXuehao)
{
int i;
if(someXuehao<=index[0].maxKey)
return index[0].address;
if(someXuehao>=index[length-1].maxKey)
return index[length-1].address;
for(i=0;i<length-1;i++)
{
if(someXuehao>index[i].maxKey && someXuehao<=index[i+1].maxKey)
return index[i+1].address;
}
return -1;
}
int SequencSearch(struct student students[],int startIndex,int length, int someXuehao)
{
//顺序查找,在students[]中查找someXuehao,表长length
//成功返回位置(0~length-1),失败返回-1。
int i;
for(i=startIndex;i<startIndex+length;i++)
{
if(students[i].Xuehao==someXuehao)
return i;
}
return -1;
}
void main()
{
struct student s[15]={1,11,2,22,3,33,4,44,5,55,
6,66,7,77,8,88,9,99,10,1000,
11,110,12,120,13,130,14,140,15,150};
struct indexNode index[3]={0,5,5,10,10,15};
int insertBlockIndex, searchPosition;
insertBlockIndex=SearchPosition(index,3,16);
printf("\ninsertBlockIndex:%d",insertBlockIndex);
searchPosition=SequencSearch(s,insertBlockIndex,5,16);
printf("\n下标为:%d\n",searchPosition);
}
分类:
[18] 数据结构与算法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY