c++ - High performance heap sorting -
i have vector of size more 5 million, each time pick 1 element smallest key vector, , process on element. process particular element, of remaining elements in vector affected key update. next time if want pick element smallest key vector, must sort vector once again. problem number of picking-up smallest element vector high 0.5 million, program runs slow. clearer understanding, write following code illustrate:
void function(vector<myobj*>& a) { //a.size() near 5 million, maybe more such 50 million. make_heap(a.begin(), a.end(), compare); // compare function self-defined. (int i=0; i<500000; i++) { myobj* smallest_elem = a.front(); pop_heap(a.begin(), a.end()); a.pop_back(); process_myobj(smallest_elem); // here of elements // in affect, causing // keys changed. make_heap(a.begin(), a.end()); // since elements' keys in changed, // heap sorting once again // necessary in viewpoint. } } is there ways make code run efficient possible? idea welcome, not limited improvement in algorithm, example, parallel or else. thank much!
if process_myobj indeed affecting keys of elements in a, don't think there can do. if modified of keys, write code update individual elements in heap.
as code don't see gain building heap. linear scan find minimal element, swap last one, , pop last element.
Comments
Post a Comment