Question: select the top 10 largest N numbers for ordered output. N up to 100 billion. Each number range is 0-2147483647 author: goosman. leimail: lgg860911@yahoo.com.cnblog: http://blog.csdn.net/lgg201 php test results: input 1 million total [1000000] total input comparison [2001653] Total write memory [552] total time consumption [1.742764 s] php Solution: [php] <? Php define ('debug', FALSE); define ('info', TRUE); $ stderr = fopen ('php: // stderr', 'W + '); $ stdout = fopen ('php: // stdout ', 'W +'); $ stdin = fopen ('php: // stdin ', 'r + '); class PQueue {public $ data; public $ next = NULL; public function _ construct ($ data) {$ this-> data = $ data ;} public static function factory ($ data, $ n) {$ I =-1; $ head = NULL; $ prev = NULL; while (++ $ I <$ n) {$ node = new PQueue ($ da Ta); if (is_null ($ head) $ head = $ node; if (! Is_null ($ prev) $ prev-> next = $ node; $ prev = $ node;} return $ head;} public static function dump ($ node, $ n) {global $ stderr, $ stdout; while (! Is_null ($ node) {fprintf ($ n? $ Stderr: $ stdout, "% d \ n", $ node-> data); $ node = $ node-> next;} if ($ n) fprintf ($ n? $ Stderr: $ stdout, "\ n") ;}} function generate_test_data ($ n) {global $ stderr, $ stdout; srand (time ()); for ($ I = 0; $ I <$ n; $ I ++) {$ r = rand (0, 2147483647); fprintf ($ stdout, "% d \ n", $ r); fprintf ($ stderr, "% s", pack ('l', $ r) ;}} function main ($ argc, $ argv) {global $ stderr, $ stdout, $ stdin; if ($ argc <2) {printf ("usage: \ n \ t1. Generate Test Data: % s <number>/* standard errors: test data is output in binary format, and test data is output in text format for scripts. Verify */\ n \ t2. run the Top 10 search: % s <exec>/* the Top 10 maximum data output from the standard output (in reverse order ), when INFO is enabled, statistics are output for standard errors. When DEBUG is enabled, debugging information \ n ", $ argv [0], $ argv [0]) is output. exit (0);} if (strcmp ($ argv [1], "exec ")! = 0) {/* number input error tolerance */generate_test_data ($ argv [1]); exit (0) ;}$ sbuff = NULL; $ rbuff = PQueue:: factory (-1, 10); if (DEBUG) {PQueue: dump ($ rbuff, 1);} if (INFO) {$ s_0 = 0; $ s_1 = 0; $ s_2 = 0; $ begin = microtime (TRUE);} while (FALSE! = ($ Sbuff = fread ($ stdin, 1024*1024*4) {$ sbuff = unpack ('L * ', $ sbuff); if (INFO) {$ s_2 + = count ($ sbuff);} foreach ($ sbuff as $ d) {if (INFO) {$ s_0 ++;} if (DEBUG) fprintf ($ stderr, "processing % d \ n", $ d); $ tmp = & $ rbuff; while ($ tmp! = NULL & $ d >=$ tmp-> data) {$ tmp = & $ tmp-> next; if (INFO) {$ s_0 + = 2 ;}} if (INFO) {$ s_0 ++;} if ($ tmp = $ rbuff) continue; if (DEBUG) fprintf ($ stderr, "tmp % d, rbuff % d \ n ", is_null ($ tmp )? -1: $ tmp-> data, $ rbuff-> data); if (INFO) {$ s_0 ++; $ s_1 ++ ;} $ rbuff-> data = $ d; if ($ tmp! = $ Rbuff-> next) {$ t = $ rbuff; $ rbuff = $ rbuff-> next; $ t-> next = is_null ($ tmp )? NULL: $ tmp; $ tmp = $ t; if (INFO) {$ s_1 + = 4; $ s_0 ++ ;}} if (DEBUG) PQueue :: dump ($ rbuff, 1);} if (INFO) {$ end = microtime (TRUE);} PQueue: dump ($ rbuff, 0); if (INFO) {fprintf ($ stderr, "Total [% d] input \ n total comparison [% d] times \ n total write memory [% d] times \ n total time consumed [% 0.6fs] \ n ", $ s_2, $ s_0, $ s_1, $ end-$ begin) ;}} main ($ argc, $ argv );