Timeline for Find the 1st and 2nd largest num in an array, O(log N) + O(N) version
Current License: CC BY-SA 3.0
3 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Sep 19, 2016 at 17:29 | comment | added | Ext3h |
Oh, and the signature of algo1 should be changed to vector<int> algo1(const vector<int> &src_data) . That copy of src_data is currently responsible for ~70% of the run time of that function.
|
|
Sep 19, 2016 at 16:54 | comment | added | Ext3h |
There is still room for improvement in algo1, you are doing twice as many comparisons as you would need. Only test for (src_data[i] > max1st) if (src_data[i] > max2nd) was true. Apart from that, why not using iterator, but index to access vector? (Latter one just being a matter of style, the difference should be minimal. If you want to push further though, you should use a local variable to copy src_data[i] by value.)
|
|
Sep 19, 2016 at 16:45 | history | answered | very hit | CC BY-SA 3.0 |