C++20 sections · 1024 units
Open in Course

sort Basics

Default sorting

I'll show you sort(v.begin(), v.end()) to arrange elements in ascending order. The function takes two iterators marking the start and end of the range to sort. You can sort part of a vector: sort(v.begin(), v.begin() + 5) sorts only the first five elements.

Elements beyond position 5 remain in their original order. Time complexity is O(nlogn)O(n \log n), making it efficient even for large arrays. You'll use this algorithm more frequently than any other in competitive programming.