Click here to Skip to main content
15,902,112 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: find the largest 1000 values Pin
David Crow9-Nov-07 2:43
David Crow9-Nov-07 2:43 
GeneralRe: find the largest 1000 values Pin
George_George9-Nov-07 3:11
George_George9-Nov-07 3:11 
GeneralRe: find the largest 1000 values Pin
David Crow9-Nov-07 5:57
David Crow9-Nov-07 5:57 
GeneralRe: find the largest 1000 values Pin
George_George9-Nov-07 21:23
George_George9-Nov-07 21:23 
QuestionRe: find the largest 1000 values Pin
David Crow10-Nov-07 4:03
David Crow10-Nov-07 4:03 
AnswerRe: find the largest 1000 values Pin
George_George10-Nov-07 4:13
George_George10-Nov-07 4:13 
QuestionRe: find the largest 1000 values Pin
David Crow16-Nov-07 9:36
David Crow16-Nov-07 9:36 
AnswerRe: find the largest 1000 values Pin
BadKarma7-Nov-07 21:28
BadKarma7-Nov-07 21:28 
Hi,

I didn't check the correctness of the answers above. But you might use the auto sort mechanisme of std::map.
std::map<int, int> theMap;
  int data[10];

  data[0] = 1;
  data[1] = 7;
  data[2] = 3;
  data[3] = 8;
  data[4] = 1;
  data[5] = 9;
  data[6] = 12;
  data[7] = 11;
  data[8] = 13;
  data[9] = 6;

  for(int iIndex = 0; iIndex < 10; iIndex++)
  {
    if(theMap.size() < 4) // LIMIT TO THE 4 HIGHEST VALUES
    {
      theMap[data[iIndex]] = data[iIndex]; // Add first 4 items
    }
    else
    {
      // if the new data is higher then the lowest on in the map 
      // replace it with the new data
      if(data[iIndex] > theMap.begin()->second)
      {
        theMap[data[iIndex]] = data[iIndex];

        //  remove first
        theMap.erase(theMap.begin());
      }
    }
  }


I don't know if this is performant on millions of data, but its a simple straightforward algorithm .

codito ergo sum

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

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.