Compute Median of Values Stored In Vector – C++?

There is no need to completely sort the vector: std::nth_element can do enough work to put the median in the correct position. See my answer to this question for an example.

Of course, that doesn’t help if your teacher forbids using the right tool for the job.

Leave a Comment