
1 Mar
2013
1 Mar
'13
4:31 p.m.
Thx, but are you sure? Median is defined as the middle element in odd distributions but is the average of the 2 middle elements in even distributions.
So? For a collection c of size 2n, you would use:
nth_element( begin(c), begin(c) + n - 1, end(c) ); nth_element( begin(c), begin(c) + n, end(c) ); auto median = (c[n - 1] + c[n]) / 2;
Yes however nth_element has some overhead so doing it twice is not advisable. Ofc u can fix that (by either partial sort etc.), but the best thing was: auto med = boost::median(itBegin, itEnd); (//or std::)