Select the top 10 [C Language] From N

Source: Internet
Author: User

Question: select the top 10 largest N numbers for ordered output. N is up to 100 billion. The value range is 0-2147483647: input 1 million entries total [1000000] total input comparison [2001654] Total write memory [552] total time consumption [0.014687 s] C language version solution [cpp] # include <stdio. h> # include <time. h> # include <stdlib. h> # include <unistd. h> # include <strings. h>/** author: goosman. lei * mail: lgg860911@yahoo.com.cn * blog: http://blog.csdn.net/lgg201 */# Define BUFF_LEN (4096)/* # define DEBUG */# define INFO typedef struct queue_s queue_t; struct queue_s {int data; queue_t * next ;}; static void generate_test_data (long n) {long I; int r; int l; l = sizeof (int); srand (time (NULL); for (I = 0; I <n; I ++) {r = rand (); fprintf (stdout, "% d \ n", r); write (STDERR_FILENO, & r, l) ;}} static int read_input (int fd, void * buff, int buff_len) {int I, Ret; for (I = 0; I <buff_len;) {ret = read (fd, buff, BUFF_LEN); if (-1 = ret) {perror ("read error \ n"); exit (0);} else if (0 = ret) {break;} else {buff + = ret; I + = ret ;}} return I;} static void dump_link (queue_t * q, int n) {for (; q! = NULL; q = q-> next) fprintf (n? Stderr: stdout, "% d \ n", q-> data); if (n) printf ("\ n");} int main (int argc, char * argv []) {int * sbuff; int I, j, n; queue_t * rbuff, ** tmp, * t; # ifdef INFO int s_0, s_1, s_2; struct timeval begin, end; # endif if (argc <2) {printf ("usage: \ n \ t1. Generate Test Data: % s <number>/* The standard error outputs the test data in binary mode, and the standard output outputs the test data in text mode for script verification */\ n \ t2. execute the Top 10 search: % s <exec>/* maximum data (in descending order) before standard output. When INFO is enabled, statistics are output in standard errors. When DEBUG is enabled Error output debugging information \ n ", argv [0], argv [0]); return (0);} if (strcmp (argv [1]," exec ")! = 0) {/* digit input error tolerance */generate_test_data (atoi (argv [1]); return 0 ;} sbuff = malloc (1024*1024*4); rbuff = malloc (sizeof (queue_t) * 10);/* buffer initialization */bzero (rbuff, sizeof (queue_t) * 10); for (I = 0; I <9; I ++) {(rbuff + I)-> next = (rbuff + I + 1 ); (rbuff + I)-> data =-1;} (rbuff + 9)-> data =-1; (rbuff + 9)-> next = NULL; # ifdef DEBUG dump_link (rbuff, 1); # endif # ifdef INFO s_0 = 0; s_1 = 0; s_2 = 0; gettimeofday (& begin, NULL); # endif while (0! = (N = read_input (STDIN_FILENO, sbuff, 1024*1024*4) {# ifdef INFO s_0 ++; # endif j = n/4; # ifdef INFO s_2 + = j; # endif for (I = 0; I <j; I ++) {# ifdef INFO s_0 ++; # endif # ifdef DEBUG fprintf (stderr, "processing % d \ n", * (sbuff + I); # endif for (tmp = & rbuff; * tmp! = NULL & * (sbuff + I)> = (* tmp)-> data;) {tmp = & (* tmp)-> next ); # ifdef INFO s_0 + = 2; # endif} # ifdef INFO s_0 ++; # endif if (* tmp = rbuff) {continue;} # ifdef DEBUG fprintf (stderr, "tmp % d % p, rbuff % d % p \ n", (* tmp) = NULL? -1: (* tmp)-> data, * tmp, rbuff-> data, rbuff); # endif rbuff-> data = * (sbuff + I ); # ifdef INFO s_1 ++; s_0 ++; # endif if (* tmp! = Rbuff-> next) {t = rbuff; rbuff = rbuff-> next; t-> next = NULL = * tmp? NULL: * tmp; * tmp = t; # ifdef INFO s_1 + = 4; s_0 ++; # endif} # ifdef DEBUG dump_link (rbuff, 1 ); # endif }# ifdef INFO gettimeofday (& end, NULL); # endif dump_link (rbuff, 0); # ifdef 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. TV _sec * 1000000 + end. TV _usec-begin. TV _sec * 1000000-begin. TV _usec)/1000000.0); # endif return 0 ;}

Related Article

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.