数据结构——外部排序

之前有写过内部排序,这次看到严蔚敏老师的书上还介绍了外部排序,就一起记录一下,以便以后可以看看:

1.外部排序

外部排序是指数据量很大,一下子不能将所有的数据放入内存里面进行排序,只能一部分一部分从硬盘中读取数据,送入内部排序,将内部排序的结果取出,换下一批数据进行排序。
对于外部排序的提高的核心问题是:减少外部存储读写的次数。在一般情况下,对m个初始归并段进行K-路平衡归并,归并的趟数:s=以k为底,m的对数结果的向上取整值。

2.多路平衡归并的实现

由于K的增加能够减少m便能减少存储读写的次数,但是k的增加,内部归并时间也会增大,这样不是我们所希望的。所以我们在内部排序的时候如果能选择一种算法将不随K的选择而增加内部排序的时间的话,那就是比较好的,这就是”败者树“,看一下败者树的定义:在对一个序列进行初始堆的建立,然后每次让子孩子的大的去上一次,这样的过程就是构建一个败者树。
败者树的核心代码:
void Adjust(int s)
{
	int t=(s+k)/2;
	int temp;
	while(t>0)
	{
		if(External[s] > External[LoserTree[t]])
		{
			temp = s;
			s = LoserTree[t];
			LoserTree[t]=temp;
		}
		t=t/2;
	}
	LoserTree[0]=s;
}

void CreateLoserTree()
{
	External[k]=MINKEY;
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
先让我们看看原题的三个任务介绍: 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.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值