码迷,mamicode.com
首页 > 编程语言 > 详细

给一千万个数据量的文件排序

时间:2015-08-30 23:16:49      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <set>
#include <bitset>
#include <ctime>
#include <cassert>
#include <climits>
#include <algorithm>
#define MAXN 5000000
#define RST(N)memset(N, 0, sizeof(N))
using namespace std;

typedef unsigned long long ULL;

int main()
{
    bitset <MAXN> bit_map;
    bit_map.reset();

    //time record
    clock_t begin = clock();

    //read data from data.in file
    FILE *wait_sort_file = fopen("data.in", "r");
    assert(wait_sort_file);
    ULL Num;
    while(~fscanf(wait_sort_file, "%d ", &Num))
    {
        if(Num < MAXN) bit_map.set(Num, 1);
    }
    //write data to data.out file
    FILE *sorted_file = fopen("data.out", "w");
    assert(sorted_file);
    for(int i=0; i<MAXN; i++)
    {
        if(bit_map[i] == 1) fprintf(sorted_file, "%d ", i);
    }
    //seek the offset of file
    int offset = fseek(wait_sort_file, 0, SEEK_SET);
    if(offset) perror("fseek fail");
    else
    {
        //read data again
        bit_map.reset();
        while(~fscanf(wait_sort_file, "%d ", &Num))
        {
            if(Num >= MAXN) bit_map.set(Num-MAXN, 1);
        }
        //write again
        for(int i=0; i<MAXN; i++)
        {
            if(bit_map[i] == 1)
                fprintf(sorted_file, "%d ", i+MAXN);
        }
    }

    clock_t end = clock();
    cout << "cost time: " << (double)(end-begin)/CLOCKS_PER_SEC << endl;

    fclose(wait_sort_file);
    fclose(sorted_file);
    return 0;
}

 

 

 


 

版权声明:感觉我写的还算不错的的话希望你能够动动你的鼠标和键盘为我点上一个赞或是为我奉献上一个评论,在下感激不尽!_______________________________________________________欢迎转载,希望在你转载的同时,添加原文地址,谢谢配合

给一千万个数据量的文件排序

标签:

原文地址:http://blog.csdn.net/u011225629/article/details/48109639

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!