大数据量的处理 一亿个数排序

有1亿个浮点数,请找出其中最小的10000个。提示:假设每个浮点数占4个字节,1亿个浮点数就要站到相当大的空间,因此不能一次将全部读入内存进行排序。
问题分析:
1) 1亿个浮点数,其数据大小为 400 M。如此规模的排序,首先想到分批处理。每次读取 1 000 000 个数据并进行快速排序。需要的内存空间为 1 000 000 * 4  = 4M。需要100 次这样的排序。

2)完全没的规律的数据,考虑使用快速排序。快速排序的平均复杂度是 O( Nlog(N) )。我们可以直接使用 stl 提供的全局函数 sort() , 它使用了快速排序算法(实际是三平均分区法 median-of-three )。
3) 最后只要最大的 10000 个。则每个批次只需要保留排序结果的前 10000 个数据。这段数据已经是分段有序的。数据量为 10000 * 100。
解法:
1) 数据结构定义:
定义数据规模:

  1. enum 
  2.     batchCapacity = 1000000, 
  3.     batchCount    = 100, 
  4.     resultCount    = 10000 

2)生成 数据样本:

  1. void dataPrepare( const char* filName  ) 
  2.     float* pbuf; 
  3.     if( ( pbuf= (  float *)malloc( batchCapacity  * sizeof( float ) )) == NULL ) 
  4.     { 
  5.         throw( "failed to malloc" ); 
  6.     } 
  7.     //  生成 batchCapacity * batchCount 个随机实数,并保存到文件  
  8.     ofstream fout; 
  9.     fout.exceptions(std::ios::badbit | std::ios::failbit | std::ios::eofbit ); 
  10.     fout.open( filName, ios::binary ) ; 
  11.     if ( !fout   ) 
  12.     { 
  13.         throw( "file not exits" ); 
  14.     } 
  15.     for( size_t index = 0; index< batchCount; index++ ) 
  16.     { 
  17.         for( size_t index = 0; index <batchCapacity; index++  ) 
  18.         { 
  19.             pbuf[index] = RandomFloat( 0, 65537 ); 
  20.         } 
  21.         fout.write( (char*)pbuf, batchCapacity * sizeof( float )  ); 
  22.     } 
  23.     fout.close(); 
  24.     delete pbuf; 
  25.     pbuf = NULL
  26.     return ; 

以上,用 RandomFloat() 生成随机数。其定义如下:

  1. /********************************************
  2. * Rand::rand 线性同余算法获得随机数
  3. * 会循环出现相同的数。有待改进
  4. *
  5. *********************************************/ 
  6.  
  7. #include <cstdlib> 
  8. #include <ctime> 
  9. class Rand 
  10. public
  11.     static longlong r; 
  12.     static int rand()//产生随机数 
  13.     { 
  14.        // 三个参数的取值 关键字:辗转相除 二次同余 
  15.         r = ( r * 1010557   + 79390691  ) %  100663363 ;   
  16.         return r; 
  17.     } 
  18. }; 
  19. long long Rand::r = 43215; 
  20.  
  21. float RandomFloat( float low,float high) { 
  22.     float d = float( Rand::rand()) / (float(RAND_MAX) + 1); 
  23.         return low + d * (high - low); 

3 ) 排序

  1. void dataOrder( constchar* filName  ) 
  2.     float* pbuf  = (  float *)malloc( batchCapacity  * sizeof(float ) ); 
  3.     if ( pbuf == NULL ) 
  4.     { 
  5.         throw( "failed to malloc " ); 
  6.     } 
  7.     ifstream fin; 
  8.     ofstream fout; 
  9.     fin.exceptions(std::ios::badbit | std::ios::failbit | std::ios::eofbit ); 
  10.     fout.exceptions(std::ios::badbit | std::ios::failbit | std::ios::eofbit ); 
  11.     fin.open( filName, ios::binary ); 
  12.     fout.open( string( string(filName).append(".order") ).c_str(), ios::binary );  
  13.     for( size_t index = 0;index < batchCount;index++ ) 
  14.     { 
  15.         // 分批读入,排序 
  16.         fin.read( (char*)pbuf, batchCapacity *sizeof( float )  ); 
  17.         std::sort( pbuf, pbuf + batchCapacity ); 
  18.         fout.write( (char*)pbuf,  resultCount *sizeof( float ) ); 
  19.         cout <<  "writed bytes:"  << resultCount * index + 1 <<  endl; 
  20.     } 
  21.     fin.close(); 
  22.     fout.close(); 
  23.     delete pbuf; 
  24.     // 将分组的数据综合排序 
  25.     pbuf =  (  float *)malloc( resultCount * batchCount *sizeof( float ) ); 
  26.     if ( pbuf == NULL ) 
  27.     { 
  28.         throw( "failed to malloc " ); 
  29.     } 
  30.     fin.open( string( string(filName).append(".order") ).c_str(), ios::binary );  
  31.     fin.read( (char*)pbuf, resultCount * batchCount *sizeof( float )  ); 
  32.     std::sort( pbuf, pbuf +  resultCount * batchCount  ); 
  33.  
  34.     //merge_sort<float>(  pbuf,0,( resultCount *  batchCount ) - 1 ); 
  35.     // 输出 
  36.     forsize_t index = 0; index < resultCount; index++ ) 
  37.     { 
  38.         printf( "%d\t%f\n",  index, pbuf[index ] ); 
  39.     } 
  40.     fin.close(); 
  41.     delete pbuf; 
  42.     pbuf = NULL; 

性能测试结果: p4 的 cpu,每秒大约处理 30万个记录。
整个程序:
  1. const char* filName ="c:\\float.df"
  2. void dataPrepare( constchar* filName  ); 
  3. void helpInfo(); 
  4. void dataOrder( constchar* filName  ); 
  5. int main( int argc,char* argv[] ) 
  6.     try 
  7.     { 
  8.         if  ( argc == 1 ) 
  9.         { 
  10.             helpInfo(); 
  11.             return 0; 
  12.         } 
  13.         const char* filename = argv[1]+ 2; 
  14.         if ( filename != NULL && strlen( filename ) > 0 ) 
  15.         { 
  16.             filName = filename; 
  17.         } 
  18.         switch ( argv[1][1] ) 
  19.         { 
  20.         case 'g'
  21.             dataPrepare( filName  ); 
  22.             break
  23.  
  24.         case 'o'
  25.             dataOrder( filName  ); 
  26.             break
  27.         default
  28.             helpInfo(); 
  29.             return 0; 
  30.             break
  31.         } 
  32.     } 
  33.     catchconstchar* e) 
  34.     { 
  35.         cout <<  e << endl; 
  36.     } 
  37.     catch(    ) 
  38.     { 
  39.         cout <<  "unknown error" << endl; 
  40.     } 
  41.     system( "pause" ); 
  42.     return 0; 

Referance:
快速排序
the c++ programming lanauage, by bjarne stroustrup chapter 18: Algorithms and Function Objects
线性同余法生成随机数
Introduction to Algorithms, Second Edition,by Thomas H. Cormen, Charles E. 11.3 Hash functions 介绍了线性同余法的原理和用法。

三平均分区法:
Introduction to Algorithms, Second Edition,by Thomas H. Cormen, Charles E. Problems 7-5: Median-of-3 partition

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值