What is the time complexity of std::sort() in the C++ standard library?

Hari Chaudhary picture Hari Chaudhary · Dec 19, 2010 · Viewed 54.6k times · Source

What is the complexity of std::sort() in the C++ Standard Library? Which sort is applied? Is there any rule of applying any particular sorting algorithm there?

Answer

James McNellis picture James McNellis · Dec 19, 2010

Before C++11:

std::sort must have average case linearithmic (n log n) time complexity. Any algorithm may be used so long as that time complexity requirement is met. There is no worst case time complexity requirement.

If you want a guaranteed worst case time complexity function, use std::stable_sort, which has quasilinear worst case time complexity (n log^2 n).