数据结构之外部排序:归并排序法

思维导图:

在这里插入图片描述

外部归并排序的原理:

第一步:
在这里插入图片描述在这里插入图片描述第二步:
在这里插入图片描述

问题:内存缓存区大小固定,外存数据元素分块后仍然无法将俩块放入比较
答:因为归并段已经块内有序,所以只需要将归并段部分装入内存,比较每个归并段相同位置元素的先后次序写入结果集即可
例:有俩个归并段1358和2467,每个缓存区可以存放2个数据元素
1、先将俩个个归并段的前俩个数据元素写入内存
2、然后12比较输出1,缓存区1标记后移;23比较输出2,标记后移;
3、输出缓存区满,写入外存
4、然后34比较输出3,缓存区1比较完毕清空,将后俩个数据元素放入继续比较
5、重复上述的过程直到比较结束
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

外部归并排序的性能:

在这里插入图片描述

归并段个数 * 每个归并段内部排序时间 + 磁盘IO读写的次数 * 每个归并块读写的时间 + 归并趟数 * 比较次数

3:排序成归并块的读写、俩次归并排序的读写
(4+4) : 四次读 + 四次写

ps: IO读写的时间 >> 内部排序时间,所以优化外部归并排序,就要减少IO读写次数
问题: 如何减少IO读写次数?
答: 二路归并排序变四路归并排序
在这里插入图片描述

总时间主要是受 外存读写时间的控制,而外存读写时间归并趟数的影响,所以,要想减少总时间,就要减少归并趟数(多路归并)

在这里插入图片描述

归并排序法的优化:

1、让K值增大

1、即增加归并路数(会增加关键字对比次数,即增加内部排序时间)
2、用败者树减少关键字对比次数

2、让r减小

1、增大每块缓冲区容量
2、用“置换-选择排序”减少初始归并段数量

  • 15
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
先让我们看看原题的三个任务介绍: Task 1: Sorting the LINEITEM table by External Merge Sort Consider two cases: 1) using 5 buffer pages in memory for the external merge sort; 2) using 129 buffer pages in memory for the external merge sort. In the implementation, each buffer page occupies 8K bytes. The ORDERKEY attribute of the LINEITEM table is assumed to be the sort key in the external merge sort. Please report the number of passes and also the running time of the external merge sort in each case. Task 2: Organizing the sorted LINEITEM table into disk pages Please use the page format for storing variable-length records to organize the LINEITEM table sorted in Task 1. In the implementation, each disk page occupies 1K bytes. For each page we maintain a directory of slots, with a pair per slot. Both “record offset” and “record length” are 4 bytes wide. Task 3: Building a B-Tree over LINEITEM disk pages by Bulk Loading. Please use bulk loading to build a B-Tree over the disk pages of the LINEITEM table, which are generated in Task 2. The ORDERKEY attribute of the LINEITEM table is used as the (search) key for building the B-Tree. In the B-Tree, each internal node corresponds to a page of 1K bytes, both key and pointer are 4 bytes wide. Please report the running time of the bulk loading. A query interface is required for checking the B-Tree. For a reasonable ORDERKEY value, please print out all the pages visited along the path to find the corresponding record. Please also report the running time of the search.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值