std::sort
is used for sorting the elements present within a container. One of the variants of this is
std::partial_sort
, which is used for sorting not the entire range, but only a sub-part of it. It rearranges the elements in the range [first, last), in such a way that the elements before middle are sorted in ascending order, whereas the elements after middle are left without any specific order. It can be used in two ways as shown below:
- Comparing elements using <: Syntax:
Template void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last); first: Random-Access iterator to the first element in the container. last: Random-Access iterator to the last element in the container. middle: Random-Access iterator pointing to the element in the range [first, last), that is used as the upper boundary for the elements to be sorted. Return Value: It has a void return type, so it does not return any value.
Output:CPP // C++ program to demonstrate the use of // std::partial_sort #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v = { 1, 3, 1, 10, 3, 3, 7, 7, 8 }, i; vector<int>::iterator ip; // Using std::partial_sort std::partial_sort(v.begin(), v.begin() + 3, v.end()); // Displaying the vector after applying // std::partial_sort for (ip = v.begin(); ip != v.end(); ++ip) { cout << *ip << " "; } return 0; }
1 1 3 10 3 3 7 7 8Here, only first three elements are sorted from first to middle, and here first is v.begin() and middle is v.begin() + 3, and rest are without any order. - By comparing using a pre-defined function: Syntax:
Template void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp); Here, first, middle and last are the same as previous case. comp: Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines. The function shall not modify any of its arguments. This can either be a function pointer or a function object. Return Value: It has a void return type, so it does not return any value.
Output:CPP // C++ program to demonstrate the use of // std::partial_sort #include <iostream> #include <algorithm> #include <vector> using namespace std; // Defining the BinaryFunction bool comp(int a, int b) { return (a < b); } int main() { vector<int> v = { 1, 3, 1, 10, 3, 3, 7, 7, 8 }, i; vector<int>::iterator ip; // Using std::partial_sort std::partial_sort(v.begin(), v.begin() + 3, v.end(), comp); // Displaying the vector after applying // std::partial_sort for (ip = v.begin(); ip != v.end(); ++ip) { cout << *ip << " "; } return 0; }
1 1 3 10 3 3 7 7 8
Where can it be used ?
- Finding the largest element: Since, with std::partial_sort, we can partially sort the container till whichever position we would like to. So, if we just sort the first position and use a function object , we can find the largest element, without having to sort the entire container.
Output:CPP // C++ program to demonstrate the use of // std::partial_sort #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector<int> v = { 10, 45, 60, 78, 23, 21, 30 }; vector<int>::iterator ip; // Using std::partial_sort std::partial_sort(v.begin(), v.begin() + 1, v.end(), greater<int>()); // Displaying the largest element after applying // std::partial_sort ip = v.begin(); cout << "The largest element is = " << *ip; return 0; }
The largest element is = 78 - Finding the smallest element: Similar to finding the largest element, we can also find the smallest element in the container in the previous example.
Output:CPP // C++ program to demonstrate the use of // std::partial_sort #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector<int> v = { 10, 45, 60, 78, 23, 21, 3 }; vector<int>::iterator ip; // Using std::partial_sort std::partial_sort(v.begin(), v.begin() + 1, v.end()); // Displaying the smallest element after applying // std::partial_sort ip = v.begin(); cout << "The smallest element is = " << *ip; return 0; }
The smallest element is = 3
Point to remember:
- std::sort() vs std::partial_sort(): Some of you might think that why are we using std::partial_sort, in place we can use std::sort() for the limited range, but remember, if we use std::sort with a partial range, then only elements within that range will be considered for sorting, while all other elements outside the range will not be considered for this purpose, whereas with std::partial_sort(), all the elements will be considered for sorting.
CPP // C++ program to demonstrate the use of // std::partial_sort #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector<int> v = { 10, 45, 60, 78, 23, 21, 3 }, v1; int i; v1 = v; vector<int>::iterator ip; // Using std::partial_sort std::partial_sort(v.begin(), v.begin() + 2, v.end()); // Using std::sort() std::