Click here to Skip to main content
15,888,461 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: find the largest 1000 values Pin
George_George7-Nov-07 21:54
George_George7-Nov-07 21:54 
GeneralRe: find the largest 1000 values Pin
BadKarma7-Nov-07 22:42
BadKarma7-Nov-07 22:42 
GeneralRe: find the largest 1000 values Pin
George_George8-Nov-07 3:52
George_George8-Nov-07 3:52 
QuestionRe: find the largest 1000 values Pin
shpid3r8-Nov-07 6:10
shpid3r8-Nov-07 6:10 
AnswerRe: find the largest 1000 values Pin
George_George8-Nov-07 21:08
George_George8-Nov-07 21:08 
GeneralRe: find the largest 1000 values Pin
David Crow9-Nov-07 2:47
David Crow9-Nov-07 2:47 
GeneralRe: find the largest 1000 values Pin
George_George9-Nov-07 3:14
George_George9-Nov-07 3:14 
Answerhere is my algorithm. [modified] Pin
chandu00410-Nov-07 1:02
chandu00410-Nov-07 1:02 
hai george.
here i have worked it out using linked lists and iam posting the algorithm here.
it took 30 seconds (approx) to find the largest 1000 values from a chunk of 10 crore random values.
iam waiting for your comments to improvise the logic.
<small>and my sincere apologies to the members if my post is too big</small>.Big Grin | :-D

struct node
{
struct node *prev;
unsigned long value;
struct node *next;
};
struct node *ll,*prevnode,*first,*last;
ll=(struct node *)malloc(sizeof(struct node));
ll->prev=NULL;
ll->value=0;
first=ll;
prevnode=ll;
//creating 1000 nodes and inserting 0s in all the nodes.
for(int i=1;i<1000;i++)
{
struct node *newnode;
newnode =(struct node *)malloc(sizeof(struct node));
prevnode->next=newnode;
newnode->prev=prevnode;
newnode->next=NULL;
newnode->value=0;
prevnode=newnode;
}
last=prevnode;
// reading the values from the file and inserting them at the appropriate slots.
FILE *fp;
fp=fopen("E:\\chandu\\values","rb");
for(;;)
{
unsigned long j;
fread(&j,4,1,fp);
if(feof(fp))
break;
if(j<=last->value)
{
//do nothing. just ignore it because, it nomore fits into the top 1000 list.
}//end if
else if(j>=first->value)
{
//then it is the most largest element sofar, and add it at the beginning
struct node *newnode;
newnode =(struct node *)malloc(sizeof(struct node));
newnode->prev=NULL;
newnode->value=j;
newnode->next=first;
first->prev=newnode;
first=newnode;
//deleting the last node, since a new element has entered the list.
struct node *temp;
temp=last;
last->prev->next=NULL;
last=last->prev;
free(temp);
}//end else if.
else
{
//traversing till the appropriate place and inserting it there.
struct node *curnode;
curnode=first;
while(curnode->value>j)
curnode=curnode->next;
struct node *newnode;
newnode =(struct node *)malloc(sizeof(struct node));
newnode->next=curnode;
newnode->value=j;
newnode->prev=curnode->prev;
curnode->prev->next=newnode;
curnode->prev=newnode;
//deleting the last node, since a new element has entered the list.
struct node *temp;
temp=last;
last->prev->next=NULL;
last=last->prev;
free(temp);
}//end else.
}//end for

--------------------------------------------
Suggestion to the members:
Please prefix your main thread subject with [SOLVED] if it is solved.
thanks.
chandu.





-- modified at 7:21 Saturday 10th November, 2007
GeneralRe: here is my algorithm. Pin
David Crow10-Nov-07 4:05
David Crow10-Nov-07 4:05 
GeneralRe: here is my algorithm. Pin
George_George10-Nov-07 4:15
George_George10-Nov-07 4:15 
QuestionCString to TCHAR & TCHAR to CString Pin
Paulraj G6-Nov-07 20:29
Paulraj G6-Nov-07 20:29 
AnswerRe: CString to TCHAR & TCHAR to CString Pin
Cedric Moonen6-Nov-07 20:43
Cedric Moonen6-Nov-07 20:43 
GeneralRe: CString to TCHAR & TCHAR to CString Pin
Paulraj G6-Nov-07 20:59
Paulraj G6-Nov-07 20:59 
GeneralRe: CString to TCHAR & TCHAR to CString Pin
Cedric Moonen6-Nov-07 21:05
Cedric Moonen6-Nov-07 21:05 
GeneralRe: CString to TCHAR & TCHAR to CString Pin
CPallini6-Nov-07 21:14
mveCPallini6-Nov-07 21:14 
AnswerRe: CString to TCHAR; TCHAR to CString [incorrect] [modified] Pin
Llasus6-Nov-07 21:06
Llasus6-Nov-07 21:06 
GeneralHonestly Pin
CPallini6-Nov-07 21:28
mveCPallini6-Nov-07 21:28 
GeneralRe: CString to TCHAR &amp;amp; TCHAR to CString Pin
Cedric Moonen6-Nov-07 21:34
Cedric Moonen6-Nov-07 21:34 
GeneralRe: CString to TCHAR &amp;amp; TCHAR to CString Pin
Raj Prathap7-Nov-07 0:09
Raj Prathap7-Nov-07 0:09 
GeneralRe: CString to TCHAR &amp;amp; TCHAR to CString Pin
toxcct7-Nov-07 0:30
toxcct7-Nov-07 0:30 
AnswerRe: CString to TCHAR & TCHAR to CString Pin
toxcct7-Nov-07 0:28
toxcct7-Nov-07 0:28 
QuestionWEB SERVER IN VC++ [modified] Pin
kansagous6-Nov-07 19:33
kansagous6-Nov-07 19:33 
AnswerRe: WEB SERVER IN VC++ Pin
shpid3r8-Nov-07 10:14
shpid3r8-Nov-07 10:14 
QuestionHollow Brush Pin
nitin36-Nov-07 19:24
nitin36-Nov-07 19:24 
AnswerRe: Hollow Brush Pin
Neo Andreson6-Nov-07 21:36
Neo Andreson6-Nov-07 21:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.