Select the top 10 [php] _ PHP tutorials from N

Source: Internet
Author: User
Select the top 10 [php versions] from N. Question: select the top 10 largest number of N, ordered output. N maximum may reach 100 billion, each number range is 0-2147483647author: goosman. leimail: lgg860911@yahoo.com.cnblog: http: question:

Select the top 10 largest N numbers for ordered output.

N is up to 100 billion

The value range of each number is 0-2147483647.

Author: goosman. lei

Mail: lgg860911@yahoo.com.cn

Blog: http://blog.csdn.net/lgg201

Php test results:

Enter 1 million

Total [1000000] inputs

Total comparison [2001653] Times

Total write memory [552] Times

Total time consumed [1.742764 s]

Php solution:

[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 ($ 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 <$ 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 /* The standard error outputs test data in binary mode, and the standard output outputs test data in text mode for script verification */\ n \ t2. execute Top 10 lookup: % s /* The first 10 largest data records (in descending order) are output from the standard output. when INFO is enabled, statistics are output from standard errors. when DEBUG is enabled, debugging information is output from standard errors. \ n ",

$ Argv [0], $ argv [0]);

Exit (0 );

}

If (strcmp ($ argv [1], "exec ")! = 0 ){

/* Digit input error tolerance is not taken into account */

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 );

Sequence selects the top 10 values from N and outputs them in sequence. N up to 100 billion. each number range is 0-2147483647 author: goosman. lei mail: lgg860911@yahoo.com.cn blog: http :/...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.