编程珠玑习题:多趟位图排序,数字不重复出现

多趟位图排序,COUNT是趟数,内存限定在1MB,每次可排N/COUNT 趟数用N/(MEMORY*8)+1 计算
#include<iostream>
#include<fstream>
#include<string>
#include"time.h"
using namespace std;
#define N 10000000
#define BITSTEP 4
#define MASK 0x1F
#define shift 5
#define MEMORY 1000000
#define COUNT (N/(MEMORY*8)+1)
//#define COUNT(N,MEMORY) ((N)/((MEMORY) * 8) + 1)
int NN;
int bit[MEMORY/BITSTEP];

void clr(int i)
{
	bit[i>>shift]&=~(1<<(i&MASK));
}
void set(int i)
{
	bit[i>>shift]|=(1<<(i&MASK));
}
int test(int i)
{
	int tmp=bit[i>>shift]&(1<<(i&MASK));
	return tmp>>(i&MASK);
}
int main(void)
{
	
    
	string ifile,ofile;
	ifstream infile;
	ofstream outfile;
	int a;
	cin>>ifile;
	cin>>ofile;
	//cout<<COUNT(N,MEMORY)<<endl;
	cout<<(N/2)<<endl;
	NN=N/COUNT;
    cout<<NN<<endl;
    time_t str_time=time(&str_time);
	char * str=ctime(&str_time);
	cout<<"str_time"<<str<<endl;

	infile.open(ifile.c_str());
    outfile.open(ofile.c_str());
	if(!infile)
	{
		cerr<<"error: can not open file :"<<ifile<<endl;
		return -1;
	}
	if(!outfile)
	{
		cerr<<"can not open outfile :"<<ofile<<endl;
	}
	
  
	for(int k=0;k<COUNT;k++)
	{
		infile.clear();//清掉EOF
		infile.seekg(0,ios::beg);//回到文件头
		outfile.seekp(0,ios::cur);
	for(int i=0;i<NN;i++){
	//	infile<<i<<endl;
		clr(i);
	}
    while(infile>>a)
	{
		//cout<<a<<endl;
		if(a>=k*NN&&a<NN+k*NN){
		//cout<<a<<endl;
			a=a-NN*k;
		//	cout<<a<<endl;
		    set(a);
		}
	//	cout<<bit[a>>shift]<<endl;
	}
	

	time_t net_time=time(&net_time);
	char * net=ctime(&net_time);
	 double difnet=difftime(str_time,net_time);
	 cout<<"round"<<k<<endl;
	 cout<<"net_time"<<net<<difnet<<endl;
	for(i=0;i<NN;i++)
	{
		
	
		if(test(i)==1)
				//cout<<test(i)<<endl;
			outfile<<i+k*NN<<endl;
	}
	}
     time_t end_time=time(&end_time);
	 
	 char * end=ctime(&end_time);
	 double dif=difftime(str_time,end_time);
	
	//double end_time=_getsystime(&t);
     cout<<"end_time"<<end<<dif<<endl;
}

假设内存限制,需要多趟位图排序

涉及到文件的重新定位和继续读写

假若当前文件流的状态是eof,再移动读指针seekg(),将不起作用,必须先调用clear清除eof状态,才能使用seekg移动写指针

#include<iostream>
#include<fstream>
#include<string>
#include"time.h"
using namespace std;
#define N 50
#define BITSTEP 32
#define MASK 0x1F
#define shift 5
int bit[N/BITSTEP+1];

void clr(int i)
{
	bit[i>>shift]&=~(1<<(i&MASK));
}
void set(int i)
{
	bit[i>>shift]|=(1<<(i&MASK));
}
int test(int i)
{
	int tmp=bit[i>>shift]&(1<<(i&MASK));
	return tmp>>(i&MASK);
}
int main(void)
{
	string ifile,ofile;
	ifstream infile;
	ofstream outfile;
	int a;
	cin>>ifile;
	cin>>ofile;

    time_t str_time=time(&str_time);
	char * str=ctime(&str_time);
	

	infile.open(ifile.c_str());
    outfile.open(ofile.c_str());
	if(!infile)
	{
		cerr<<"error: can not open file :"<<ifile<<endl;
		return -1;
	}
	if(!outfile)
	{
		cerr<<"can not open outfile :"<<ofile<<endl;
	}

	for(int k=0;k<2;k++)
	{
		infile.clear();//清掉EOF
		infile.seekg(0,ios::beg);//回到文件头
		outfile.seekp(0,ios::cur);
	for(int i=0;i<N;i++){
	//	infile<<i<<endl;
		clr(i);
	}
    while(infile>>a)
	{
		//cout<<a<<endl;
		if(a>=k*N&&a<N+k*N){
		//cout<<a<<endl;
			a=a-N*k;
			cout<<a<<endl;
		    set(a);
		}
	//	cout<<bit[a>>shift]<<endl;
	}
	

	time_t net_time=time(&net_time);
	char * net=ctime(&net_time);
	 double difnet=difftime(str_time,net_time);
	 cout<<"round"<<k<<endl;
	 cout<<net<<difnet<<endl;
	for(i=0;i<N;i++)
	{
		
	
		if(test(i)==1)
				//cout<<test(i)<<endl;
			outfile<<i+k*N<<endl;
	}
	}
     time_t end_time=time(NULL);
	 
	 char * end=ctime(&end_time);
	 double dif=difftime(str_time,end_time);
	
	//double end_time=_getsystime(&t);
cout<<end<<":"<<":"<<str<<":"<<dif<<":"<<endl;
}

多趟位图排序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值