Click here to Skip to main content
15,916,463 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: find the largest 1000 values Pin
Cyrilix7-Nov-07 18:42
Cyrilix7-Nov-07 18:42 
GeneralRe: find the largest 1000 values Pin
George_George7-Nov-07 19:04
George_George7-Nov-07 19:04 
GeneralRe: find the largest 1000 values Pin
chandu0048-Nov-07 1:52
chandu0048-Nov-07 1:52 
GeneralRe: find the largest 1000 values Pin
George_George8-Nov-07 4:00
George_George8-Nov-07 4:00 
QuestionRe: find the largest 1000 values Pin
David Crow7-Nov-07 4:55
David Crow7-Nov-07 4:55 
AnswerRe: find the largest 1000 values Pin
Cyrilix7-Nov-07 13:25
Cyrilix7-Nov-07 13:25 
GeneralRe: find the largest 1000 values Pin
George_George7-Nov-07 17:01
George_George7-Nov-07 17:01 
GeneralRe: find the largest 1000 values [modified] Pin
Cyrilix7-Nov-07 18:37
Cyrilix7-Nov-07 18:37 
Hmm...chandu's idea might be better than this one, but you could keep a std::map of with 1000 objects, and an int smallest that represents the lowest value (this is easy to track because you can check for every number that you read, whether or not it's larger or smaller than this value). With the map, you can have a map<int, int=""> where the first int is your value, the second int is the index of the number in the array. For every number, if the value is lower or equal to int smallest, discard it and keep going. Otherwise, remove the element with the lowest value and insert a new element with the new value. In order to update the lowest value "counter", you can iterate through the map again to find it. There is only one real difference between my algorithm and chandu's algorithm. With his algorithm, everything is sorted, and you iterate through the linked list on every insertion with (I'm not entirely sure, but I think) an average case of O(n/2) time given unbounded dataset values. Please correct me if I'm wrong. With my algorithm, every insertion is done in O(1) time, but searching for the new lowest value in a dataset of 1000 objects takes O(n) time... however, this can be optimized. What you can do is keep a buffer of x lowest values and access that buffer in O(1), so as to not have to search for the new lowest value everytime you perform an insertion.


-- modified at 0:42 Thursday 8th November, 2007

Edit: I just realized you may not need to use a map at all, if there's no association required. You might as well use any other data structure that fits the purpose.
GeneralRe: find the largest 1000 values Pin
George_George7-Nov-07 19:15
George_George7-Nov-07 19:15 
GeneralRe: find the largest 1000 values [modified] Pin
Cyrilix7-Nov-07 19:29
Cyrilix7-Nov-07 19:29 
GeneralRe: find the largest 1000 values Pin
George_George7-Nov-07 20:09
George_George7-Nov-07 20:09 
GeneralRe: find the largest 1000 values Pin
Cyrilix7-Nov-07 20:27
Cyrilix7-Nov-07 20:27 
GeneralRe: find the largest 1000 values Pin
George_George7-Nov-07 20:45
George_George7-Nov-07 20:45 
GeneralRe: find the largest 1000 values Pin
Cyrilix7-Nov-07 20:53
Cyrilix7-Nov-07 20:53 
GeneralRe: find the largest 1000 values Pin
George_George7-Nov-07 21:49
George_George7-Nov-07 21:49 
GeneralRe: find the largest 1000 values Pin
Cyrilix7-Nov-07 21:52
Cyrilix7-Nov-07 21:52 
GeneralRe: find the largest 1000 values Pin
George_George7-Nov-07 22:00
George_George7-Nov-07 22:00 
GeneralRe: find the largest 1000 values Pin
Cyrilix7-Nov-07 22:46
Cyrilix7-Nov-07 22:46 
GeneralRe: find the largest 1000 values Pin
George_George8-Nov-07 3:55
George_George8-Nov-07 3:55 
AnswerRe: find the largest 1000 values Pin
George_George7-Nov-07 16:56
George_George7-Nov-07 16:56 
GeneralRe: find the largest 1000 values Pin
David Crow8-Nov-07 2:28
David Crow8-Nov-07 2:28 
GeneralRe: find the largest 1000 values Pin
George_George8-Nov-07 3:55
George_George8-Nov-07 3:55 
GeneralRe: find the largest 1000 values Pin
David Crow8-Nov-07 4:03
David Crow8-Nov-07 4:03 
GeneralRe: find the largest 1000 values Pin
George_George8-Nov-07 4:05
George_George8-Nov-07 4:05 
GeneralRe: find the largest 1000 values Pin
David Crow8-Nov-07 4:31
David Crow8-Nov-07 4:31 

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.