
第7章查找表7.1查找表的基本概念7.2静态查找表7.3动态查找表中国科学技术大学ypb@ustc.edu.cn
ypb@ustc.edu.cn 1 中国科学技术大学 第7章 查找表 • 7.1查找表的基本概念 • 7.2静态查找表 • 7.3动态查找表

7.1查找表的基本概念查找表:一由同一类元素或记录构成的集合。对数据元素间的关系未作限定对查找表的操作有一查找某个“特定”的元素是否在表中一查找某个“特点”的元素的各种属性。一在查找表中插入一个元素。一在查找表中删除一个元素静态查找表、动态查找表关键字数据元素中的某个数据项值。可以标识一个数据元素,如可以唯一标识,则为主关键字(primarykey)。查找根据给定的某个值,在查找表中确定一个关键字等于给定值的数据元素。若找到表示查找成功,返回该元素详细信息或在查找表中的位置:否则返回NULL2中国科学技术大学ypb@ustc.edu.cn
ypb@ustc.edu.cn 2 中国科学技术大学 • 查找表: – 由同一类元素或记录构成的集合。对数据元素间的关系未作限定。 • 对查找表的操作有 – 查找某个“特定”的元素是否在表中。 – 查找某个“特点”的元素的各种属性。 – 在查找表中插入一个元素。 – 在查找表中删除一个元素 • 静态查找表、动态查找表 • 关键字 – 数据元素中的某个数据项值。可以标识一个数据元素,如可以唯一 标识,则为主关键字(primary key)。 • 查找 – 根据给定的某个值,在查找表中确定一个关键字等于给定值的数据 元素。若找到表示查找成功,返回该元素详细信息或在查找表中的 位置;否则返回NULL 7.1查找表的基本概念

7.2静态查找表typedef struct?1元素数据data;Datatype//元素关键字key,KeyType)Elemtype;//数据元素类型typedef struct?Elemtype*elem;//约定从下标1开始intlen,{StaticSrhTable;//顺序静态查找表类型3中国科学技术大学ypb@ustc.edu.cn
ypb@ustc.edu.cn 3 中国科学技术大学 7.2静态查找表 typedef struct{ Datatype data; //元素数据 KeyType key; //元素关键字 }Elemtype; //数据元素类型 typedef struct{ Elemtype *elem; //约定从下标1开始 int len; }StaticSrhTable; //顺序静态查找表类型

>顺序查找算法7.1int SeqSearch(StaticSrhTable SST, KeyType kval)/*在顺序表SST中顺序查找关键字为kval的记录。若找到,则返回记录在表中的位序:否则,返回0*/放置监视哨SST.elem[O].key = kval;for (i= SST.len; SST.elem[i].key I= kval; i--); // 查找//查找结果return i,中国科学技术大学ypb@ustc.edu.cn
ypb@ustc.edu.cn 4 中国科学技术大学 ➢ 顺序查找 算法7.1 int SeqSearch(StaticSrhTable SST, KeyType kval) { /* 在顺序表SST中顺序查找关键字为kval的记录。 若找到,则返回记录在表中的位序;否则,返回0 */ SST.elem[0].key = kval; // 放置监视哨 for (i = SST.len; SST.elem[i].key != kval; i-); // 查找 return i; // 查找结果 }

例7.1在顺序查找表中查找成功和失败平均查找长度一查找过程中先后和给定值进行比较的关键字的个数的期望值ASL= ZP,Cii ZP:=1 i=1,2C,= n - i+ 1 P.=1/nASLss = 1/nZ (n - i+ 1) =(n+1)/25中国科学技术大学ypb@ustc.edu.cn
ypb@ustc.edu.cn 5 中国科学技术大学 • 例7.1在顺序查找表中查找成功和失败 • 平均查找长度 – 查找过程中先后和给定值进行比较的关键字的个 数的期望值 ASL=∑PiCi ∑Pi=1 i=1,2,。n Ci=n-i+1 Pi=1/n ASLss=1/n∑(n-i+1)=(n+1)/2

S>二分查找(顺序有序表)二分查找(binarySearch):也称折半查找。int BinSearch(StaticSrhTable SST, KeyType kval){//置查找范围初值bot=l, top=SST.len;while(botkval)top=mid-1l;//前半区//后半区else bot =mid+l;11//未查找到return O:76中国科学技术大学ypb@ustc.edu.cn
ypb@ustc.edu.cn 6 中国科学技术大学 ➢ 二分查找 (顺序有序表) 二分查找(binary Search):也称折半查找。 int BinSearch(StaticSrhTable SST, KeyType kval){ bot=1, top=SST.len; // 置查找范围初值 while(botkval) top=mid-1;//前半区 else bot =mid+1; // 后半区 } } return 0; // 未查找到 }

S丫二分查找平均查找长度(假设满二叉树)ASLbs= (n+1) /nlog(n+1)-lASLbs=(20+21*2+...+2h-1*h)Pi-= Zi * 2i-1 (n=2h-1)2 i=t+1令: S=Zi*2i-1=2hi* 2i-22 Zh-1(t + 1)2t-1=2-1 t * 2t-1+2Zh-1 2t-1=2(2h t * 2t-1 - h * 2h-1)+Zh-1 2t=2(S - h * 2h-1) + 2h - 1所以: S =h * 2h - 2h + 1 =(n + 1)log2(n+ 1) -nb==s = n+1log2(n+ 1) - 1EASLhypb@ustc.edu.cn中国科学技术大学
ypb@ustc.edu.cn 7 中国科学技术大学 二分查找平均查找长度(假设满二叉树) ASLbs=(n+1)/nlog(n+1)-1 ASLbs=(2 0+2 1*2+.+2 h-1*h)Pi=

索引(分块)查找又称索引顺序香找一介于顺序查和折半查找之间。适合于关键字分块有序typedef struct {KeyType key,intstadr,↑indexItem;typedef struct:indexItem *elem;intlength;indexTable;设索引长度b,顺序表长度为n,则:ASLidx=ASL(b)+ASL(n/b)=log2(b+1)-1+(n/b+1)/28中国科学技术大学ypb@ustc.edu.cn
ypb@ustc.edu.cn 8 中国科学技术大学 • 又称索引顺序查找 – 介于顺序查和折半查找之间。适合于关键字分块有序 typedefstruct { KeyType key; int stadr; }indexItem; typedefstruct{ indexItem *elem; int length; }indexTable; 设索引长度b,顺序表长度为n,则: ASLidx=ASL(b)+ASL(n/b)≈log2 (b+1)-1+(n/b+1)/2 索引(分块)查找

stateBlklnxSearch(StaticSrhTableSST, InxTab Inx, KeyTypekval)bot=1,top=Inx.len,blFound=FALSE://置查找范围初值Ⅱ越界if (kval > Inx.elem[top].key) return 0;while (bot kval) top = mid - 1; I/ 前半区Ⅱ后半区else bot = mid + 1;71l/退出循环时,bot所指的为所找的块bn=Inx.elem[bot].StartAdd;/第bot块的数据记录起始地址if (bot < Inx.len) en = Inx.elem[bot + 1].StartAdd - 1;/第bot块的数据记录尾地址else en = SsT.len;for (i = bn; (i<= en) && (SST.elem[ij.key!= kval); i++);if (i <= en) return i;Ⅱ未查找到return O;1
state BlkInxSearch(StaticSrhTable SST, InxTab Inx, KeyType kval){ bot = 1, top = Inx.len, blFound = FALSE; // 置查找范围初值 if (kval > Inx.elem[top].key) return 0; // 越界 while (bot kval) top = mid - 1; // 前半区 else bot = mid + 1; // 后半区 } } //退出循环时,bot所指的为所找的块 bn = Inx.elem[bot].StartAdd; //第bot块的数据记录起始地址 if (bot < Inx.len) en = Inx.elem[bot + 1].StartAdd – 1; else en = SST.len; //第bot块的数据记录尾地址 for (i = bn; (i <= en) && (SST.elem[i].key != kval); i++); if (i <= en) return i; return 0; //未查找到 } 为什么选择bot 而不是top?

索引顺序查找索引表1726487696块内最大关键字5811518块的起始序号SST.elem74842295232766817101682126234730408696822345678910111213141516171801192010中国科学技术大学ypb@ustc.edu.cn
ypb@ustc.edu.cn 10 中国科学技术大学 索引顺序查找 索引表 17 26 48 76 96 1 5 8 15 18 块内最大关键字 块的起始序号 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 17 10 16 8 21 26 23 47 30 40 32 48 42 29 76 52 68 86 96 82 SST.elem