c/c++ 使用Bitvector 进行无重复数的排序

本文介绍了一种生成无重复随机数的数据集方法,并利用BitVector数据结构进行数据校验,确保数据集中每个整数的唯一性。通过对比两种不同方法的优劣,展示了如何在限定内存和时间条件下高效生成并验证大规模数据集。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

输入:
所输入的是一个文件,至多包含n个正整数,每个正整数都要小于n,这里n=10,000,000。如果输入时某一个整数出现了两次,就会报错。这些整数相互之间没有关系。

输出:
以升序形式输出经过排序的整数列表

约束条件:
不超过1.5M的内存空间,硬盘空间不受限制,运行时间不超过1min。使用bitvector。

首先生成数据文件,然后保存到指定文件中

//data1.cpp
/**********************
Author:Ice
Data:2020/3/3
Description:生成随机数的数据集(有不足,可能有重复数)
*********************/
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<fstream> 
using namespace std;

//宏定义函数 
#define FINITE 10000000 
#define random() (int)(((double)rand()/RAND_MAX)*FINITE)


int main()
{
	int count = 1000; // 指定生成的数据的数量 

	ofstream outputFile("data.txt"); //建立文件输出流,句柄为outputFile 
	if(!outputFile){
		cout<<"Failed to access file.";
		exit(-1);
	}
	
    srand((int)time(0));  // 用srand((int)(time(NULL))的方法及系统时钟,产生不同的随机数种子 
    
    for (int i = 0; i < count; i++)
    {
        outputFile <<" "<< random(); //将结果输出到文件中 
        
    }
	
	outputFile.close(); //关闭流文件 
	
	//以下代码主要为了复习从文件读取数据的方法
	ifstream dataInput("data.txt"); //建立文件输入流,句柄为dataInput 
	if(!dataInput){
		cout<<"Failed to access data file.";
		exit(-1);
	}
	
	while(!dataInput.eof()){
		int temp;
		dataInput>>temp; //从文件中读取int类型数据 
		cout<<temp<<" ";
	}

	dataInput.close(); //关闭流文件 
	
    return 0;
}

下面为count为 10 时的输出结果:

698568 9465315 9893490 4534134 9511703 2854091 8074892 1448408 4879909 6949980

注意:
rand()生成的最大整数(RAND_MAX【在C标准中定义】)是32767,但是我们要求使用的数据最大为10,000,000。这样生成的数据不够全面,所以使用下面的表达式将范围变为[0, 10000000):

(int)(((double)rand()/RAND_MAX)*FINITE);

但是,当我们需要的数很大时,比如该题目要求的一千万,生成的随机数中99.99%都会出现重复的数,那么就不符合题目的“同一个整数不能出现两次,否则会报错”。所以,接下来,要生成不含重复整数的数据集。

首先想到的是,在每生成一个随机数的时候就对其进行判断是否与之前生成的整数重复,但是这显然工作量太大,一点也不节约。
通过BitVector的思想,我们了解到,其里面存储的信息正是不会重复的整数。所以我们可以先构造一个含有从0-9999999的容器,然后打乱它,之后从中选择一定量的数,这些数一定没有重复的。

//data.cpp
/**********************
Author:Ice
Data:2020/3/3
Description:生成随机数的数据集(无重复数)
*********************/
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<fstream>
#include<algorithm>
#include<vector>
using namespace std;

//宏定义 
#define FINITE 10000000

int main()
{
	int count = 100000; // 指定生成的数据的数量 

	ofstream outputFile("data.txt"); //建立文件输出流,句柄为outputFile 
	if(!outputFile){
		cout<<"Failed to access file.";
		exit(-1);
	}
	
	vector<int> intContainer;
	//制造一个包含0-9999999的vector
    for (int i = 0; i < FINITE; ++i)
	{
       intContainer.push_back(i);
    }
    
    random_shuffle(intContainer.begin(), intContainer.end());
    //random_shuffle对元素序列进行随机的重新排序,在头文件#include<algorithm>中,
    //其对象是容器的迭代器,所以这里使用vector容器
	  
    for (int i = 0; i < count; i++)
    {
        outputFile <<" "<< intContainer[i]; //将结果输出到文件中     
    }
	
	outputFile.close(); //关闭流文件 
	
    return 0;
}
//bitVector.cpp
/**********************
Author:Ice
Data:2020/3/3
*********************/
#include<fstream>
#include<iostream>
#include<cstdlib>
#include"Bitvector.h"

using namespace std;

#define FINITE 10000000

int main(){
	Bitvector *bvArray = new Bitvector(FINITE); //创建Bitvecor对象 
	
	ifstream dataInput("data.txt"); //建立文件输入流,句柄为dataInput 
	if(!dataInput){
		cout<<"Failed to access data file.";
		exit(-1);
	}
	
	while(!dataInput.eof()){
		int temp;
		dataInput>>temp; //从文件中读取int类型数据 
		
		if(!bvArray->operator[](temp)) //判断重复数
			bvArray->Set(temp,1);  //之前未出现过,设置该位为1
		else{
			cout<<endl<<"The input file does not meet the requirements."<<endl;
			exit(-1);
		}
				 
	}

	dataInput.close(); //关闭流文件 
	
	ofstream resultFile("result.txt");

	for(int i = 0; i < bvArray->Size();i++){
		if(bvArray->operator[](i)){    //判断第i位是1(存在)还是0(不存在) 
			resultFile<<i<<" ";
		}
	}
	
	resultFile.close(); //关闭流文件 
	
	return 0;
}

相关代码资源已上传。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值