column_1 Cracking the Oyster 习题解答

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">序:久闻这本书,正好这几个月比较有时间,仔细读一下,把课后题要写一下,此解答仅为个人意见,欢迎讨论,大家有更好的方法可以提出,我会在文中更新并注明。为更好的表现原题,将题目的原文及翻译同时给出。</span>

1.If memory were no scarce, how would you implement a sort in a language with libraries for representing and sorting sets?

如果内存不紧缺,你将如何用一种语言(该语言可以使用库来表示和排序集合)来进行排序?

答:C++的STL

2.how would you implement bit vectors using bitwise logical operations(such as and, or and shift)?

你会如何使用位逻辑运算实现位向量(比如“与”运算,“或”运算以及“移位”运算)?

答:

#define MAX 1000
char memory[MAX]; 
void set(unsigned int x){
       memory[x/8] ||= (1<<(x%8));
}
bool get(unsigned int x){
       return memory[x/8] & (1<<(x%8));
}
3.Run-time efficiency was an important part of the design goal, and the resulting program was efficient enough. Implement the bitmap sort on your system and measure its run time; how does it compare to the system sort and to the sorts in Problem 1? Assume that n is 10,000,000, and that the input file contains 1,000,000 integers. 

运行时效率是一个重要的设计目标,由此产生的程序也具有足够的效率。请在系统中实现位图排序并测试其运行时间;与系统排序及问题1中的排序方法相比较,这种排序方法如何?假设n等于1000000,并且输入文件包含1000000个整数。

答:

#include <algorithm>
#include <ctime>
#include <fstream> 
#include <iostream>
#include <vector>

using namespace std;

void set(char* memory,unsigned int x){
	memory[x/8] |= (1<<(x%8));
}
bool check(char* memory,unsigned int x){
	return memory[x/8] & (1<<(x%8));
}

int main(){

	int count = 1000000;

	//产生count个小于count的随机数——洗牌算法
	int* data = new int[count]();
	for (int i = 0; i < count; i++)
		data[i] = i;
	int k, tmp;
	srand((unsigned)time(0));	
	for (int i = count; i > 0; i--){
		k = rand()%i;
		tmp = data[k];
		data[k] = data[i-1];
		data[i-1] = tmp;
	}

	//调用STL库 
	vector<int> vec;
	for (int i = 0; i < count; i++)
		vec.push_back(data[i]);//导入数据

	clock_t start = clock();
	sort(vec.begin(),vec.end()); //sort排序
	clock_t end = clock();
	cout << "STL sort time:" << double(end - start )/CLOCKS_PER_SEC << endl;	
	
	//bitmap排序
	char* bitmap = new char[count/8]();//新建一个bitmap
	start = clock(); //开始计时
	for (int i = 0; i < count; i++)
		set(bitmap,data[i]);  //导入所有数据
	for (int i = 0; i < count; i++){
		if (check(bitmap,i))  //导出排好序的数据
			data[i] = i;
	}
	end = clock();
	cout << "Bitmap sort time:" << double(end - start )/CLOCKS_PER_SEC << endl;		

	delete[] data;
	return 0;
}
运行结果:

4.If you take Problem 3 seriously, you will face the problem of generating k integers less than n without duplicates. The simplest approach uses the first k positive integers. This extreme data set won't alter the run time of the bitmap method by much, but it might skew the run time of a system sort. How could you generate a file of K unique random integers between 0 and n-1 in random order? Strive for a short program that is also efficient.

如果你认真做了问题3, 你将碰到生成k个小于n并且互不重复的整数的麻烦。最简单的方法就是使用前k个正整数。这个极端的数据集合不会很大的改变位图方法的运行时间,但它可能会歪曲系统排序的运行时间。你如何可以生成一个包含k个整数的文件,要求这些整数都是惟一的,并且在0到n-1之间随机出现的,次序也是随机的?请力求编写出一个简短高校的程序。

答:见Problem3中,复杂度为O(n).

5.The programmer said that he had about a megabyte of free storage, but the code we sketched uses 1.25 megabytes. He was able to scrounge the extra space without much trouble. If the megabyte had been a hard and fast boundary, what would you have recommended? What is the run time of you algorithm?

程序员说他有1MB的空闲内存,但是我们拟编写的代码有1.25MB。他可以搜集额外的空间,这一点问题都没有。那如果1MB是一个严格的分界线,你会推荐怎么办呢?你的算法所需要的运行时间是多少?




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值