user-0 提问: 关于产生数亿级随机数以及排序的问题

回复:
user: user-1
content: 这个涉及 随机数生成器，外部排序. int 32 可以表示 -2^31~2^31 之间的数据， 范围大约是 -20亿～20亿 标准库的随机数生成器，应该够用了， 不够的话自己做个，或者用赵老师的办法，拼一个 外部排序，采用归并排序吧.  虽然 亿级别的数据，现在内存是够了 但是，可用内存，通常还是没有这么多，不然可以一次性加载到内存，快速排序

user: user-2
content: 前面不是说了吗，位图法，占用512MB内存，每1bit代表一个int，一开始全置0，随机生成了某个数之后，对应位置1，直接内存排序搞定。

user: user-0
content: 引用 1 楼 user-6 的回复:是大约500m的内存吗? 直接输出二进制, 直接创建映射文件, 排序就好了能详细一点吗

user: user-2
content: 赵老师这英文...

user: user-3
content: 仅供参考：//随机产生100000000个取值范围为[0～2的32次方减1]的数据， //然后让用户输入一个数据，判断用户输入的数据是不是包含在前面随机产生的数据中。 //要求：当用户输入完成后，必须在1毫秒(千分之一秒)之内完成判断。 #include <stdio.h> #include <stdlib.h> #include <time.h> #include <malloc.h> long int i; unsigned long ul; unsigned long *d; unsigned long ulrand(void) {     return (      (((unsigned long)rand()<<24)&0xFF000000ul)     |(((unsigned long)rand()<<12)&0x00FFF000ul)     |(((unsigned long)rand()    )&0x00000FFFul)); } void main() {     d=(unsigned long *)calloc(1<<(32-5),sizeof(unsigned long));     if (NULL==d) {         printf("Can not calloc(%d,%d)!\n",1<<(32-5),sizeof(unsigned long));         return;     }     srand(time(NULL));     for (i=0;i<100000000;i++) {         while (1) {             ul=ulrand();             if (0==(d[ul>>5]&(1lu<<(ul%32)))) {                 d[ul>>5]|=1<<(ul%32);                 break;             }         }         if (0==i%1000000) printf("%09d,%10lu\n",i,ul);     }     while (1) {         printf("\nInput a number:");         fflush(stdout);         rewind(stdin);         if (1==scanf("%lu",&ul)) break;     }     if (d[ul>>5]&(1<<(ul%32))) printf("Include.\n"    );     else                       printf("Not include.\n");     free(d); } //000000000,2135468533 //001000000,2465805973 //002000000,3844079964 //003000000,1883232874 //004000000,1204697784 //005000000,4050838287 //006000000,3802081245 //007000000,1586042671 //008000000,3119931368 //009000000, 251096899 //010000000,3491239701 //011000000,3365323844 //012000000,2191846708 //013000000,1879478195 //014000000,1112631457 //015000000,1927301519 //016000000,1717332861 //017000000,2922278240 //018000000, 694854106 //019000000, 273255526 //020000000, 398518467 //021000000,3270756812 //022000000,1500289424 //023000000,1502241936 //024000000,1770380660 //025000000,3668842116 //026000000,3255869879 //027000000,1299184024 //028000000,1072990028 //029000000, 242094712 //030000000,3789344297 //031000000,2599365925 //032000000, 962754138 //033000000,2055075654 //034000000,4083452879 //035000000, 489250842 //036000000, 611455230 //037000000, 277350616 //038000000,1597410795 //039000000,3224173662 //040000000,2291446877 //041000000,2546280575 //042000000,2509145642 //043000000,2371773252 //044000000, 635555963 //045000000,2674538666 //046000000,4253690312 //047000000,2675755514 //048000000,1269320296 //049000000,3172516920 //050000000,1430265210 //051000000, 196156173 //052000000,2470825669 //053000000,2536750977 //054000000,1182829949 //055000000,3202826434 //056000000,2263336265 //057000000, 313302924 //058000000,3630264578 //059000000,1154892716 //060000000,2985304230 //061000000,1252204837 //062000000,1292076720 //063000000, 242249250 //064000000,3999999961 //065000000, 431166416 //066000000,1366947236 //067000000,1414387330 //068000000,2143784481 //069000000,3242175409 //070000000,4158065163 //071000000,1425449573 //072000000,2493600232 //073000000,1316783455 //074000000,3723170478 //075000000,3064111466 //076000000, 408557403 //077000000,3722586955 //078000000,3801652651 //079000000,3788160154 //080000000,3329440047 //081000000,1408976868 //082000000, 471838899 //083000000,2145198260 //084000000,3781081738 //085000000,3439027738 //086000000,1150808750 //087000000,2782578638 //088000000,  85604584 //089000000,2704078162 //090000000, 584840269 //091000000,3854577719 //092000000,2823653537 //093000000, 797877025 //094000000,2248017755 //095000000,1787038685 //096000000,2816548567 //097000000, 489107494 //098000000, 911680090 //099000000,3677777147 // //Input a number:3677777147 //Include.

user: user-2
content: 位图法，直接内存排序搞定。

user: user-4
content: http://blog.csdn.net/naturebe/article/details/8080083

user: user-5
content: 参考思路： http://blog.csdn.net/hustspy1990/article/details/7328013

user: user-6
content: 是大约500m的内存吗? 直接输出二进制, 直接创建映射文件, 排序就好了