多趟位图排序,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;
}
多趟位图排序