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.
More Related Contents:
- Invalid operands to binary expression while implementing vector [closed]
- Difficulties on atributting a array to another [closed]
- Erasing elements from a vector
- Why can’t I make a vector of references?
- Choice between vector::resize() and vector::reserve()
- Concatenating two std::vectors
- Does C++11 allow vector?
- When vectors are allocated, do they use memory on the heap or the stack?
- How can I create cartesian product of vector of vectors?
- vector size – 1 when size is 0 in C++
- Erasing from a std::vector while doing a for each?
- sizeof() a vector
- How to cin values into a vector
- How to use std::sort with a vector of structures and compare function?
- How to navigate through a vector using iterators? (C++)
- Initial capacity of vector in C++
- How can I use something like std::vector?
- std::vector iterator invalidation
- VBOs with std::vector
- How to make elements of vector unique? (remove non adjacent duplicates)
- Compile time triggered range check for std::vector
- What do I need to do before deleting elements in a vector of pointers to dynamically allocated objects?
- What is the right approach when using STL container for median calculation?
- How do I pass multiple ints into a vector at once?
- Does clearing a vector affect its capacity?
- Can std::vector emplace_back copy construct from an element of the vector itself?
- Is it more efficient to copy a vector by reserving and copying, or by creating and swapping? [duplicate]
- How to erase & delete pointers to objects stored in a vector?
- C++ STL: Array vs Vector: Raw element accessing performance
- C++ trying to swap values in a vector