题目:
从N个数中选取最大的前10个, 有序输出.
N最大可能达到1000亿
每个数范围是0 - 2147483647
author: goosman.lei
mail: lgg860911@yahoo.com.cn
blog: http://blog.csdn.net/lgg201
php版测试结果:
输入100万条
总计[1000000]个输入
总计比较[2001653]次
总计写内存[552]次
总计耗时[1.742764s]
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
$node = new PQueue($data);
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
$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
printf("usage: \n\t1. 生成测试数据: %s
$argv[0], $argv[0]);
exit(0);
}
if ( strcmp($argv[1], "exec") != 0 ) {
/* 不考虑数字输入的容错了 */
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, "总计[%d]个输入\n总计比较[%d]次\n总计写内存[%d]次\n总计耗时[%0.6fs]\n",
$s_2, $s_0, $s_1, $end - $begin);
}
}
main($argc, $argv);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools
