Title: Select the largest of the top 10, ordered output from the n number. n maximum may reach 100 billion per digit range is 0-2147483647 author:goosman.leimail:lgg860911@yahoo.com.cnblog:http://blog.csdn.net/lgg201 PHP test results: Input 1 million total [1000000] input total comparison [2001653] times total write memory [552] Total time spent [1.742764s] PHP version of the solution: [PHP] data = $data; The public static function factory ($data, $n) {$i =-1; $head = null; $prev = null And while (+ + $i < $n) {$node = new P Queue ($data); if (Is_null ($head)) $head = $node; if (!is_null ($prev)) $prev->next = $node; $prev = $node; } return $head; The public static function dump ($node, $n) {global $stderr, $stdout; while (!is_null ($node)) {fprintf ($n) {*. $stderr: $st Dout, "%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 = Ran D (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 /* Standard error Output test data in binary mode, standard output output test data in text mode for foot */\n\t2. Perform top 10 lookup:%s /* Standard output output first 10 maximum data (reverse), turn on info at standard error output statistics, open debug in standard error output debug information \ n ", $argv [0], $argv [0]); Exit (0); } if (strcmp ($argv [1], "exec")! = 0) {/* does not consider the fault tolerance of the digital input */Generate_test_data ($ARGV [1]); exit (0);} $sbuff = NULL; $rbuff = Pqueue::factory (-1, 10); if (DEBUG) {pqueue::d UMP ($rbuff, 1),} if (INFO) {$s _0 = 0; $s _1 = 0; $s _2 = 0; $begin = microtime (TRUE);} while ( FALSE! = ($sbuff = fread ($stdin, 1024x768 * 1024x768 * 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 (in FO) {$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::d UMP ($rbuff, 1); if (INFO) {$end = Microtime (TRUE);} Pqueue::d UMP ($rbuff, 0); if (INFO) {fprintf ($stderr, "total [%d] input \ n Total comparison [%d] times \ n Total write memory [%d] times \ n Total time spent [%0.6fs]\n], $s _2, $s _0, $s _1, $end-$begin);}} Main ($ARGC, $ARGV);
http://www.bkjia.com/PHPjc/477840.html www.bkjia.com true http://www.bkjia.com/PHPjc/477840.html techarticle Title: Select the largest of the top 10, ordered output from the n number. n maximum may reach 100 billion per digit range is 0-2147483647 author:goosman.lei mail:lgg860911@yahoo.com.cn blog:http:/...