Related Articles
std::partial_sort in C++
• Difficulty Level : Easy
• Last Updated : 06 Aug, 2017

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:

1. 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.
```
 `// C++ program to demonstrate the use of``// std::partial_sort``#include ``#include ``#include ``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;``}`

Output:

```1 1 3 10 3 3 7 7 8
```

Here, 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.

2. 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.
```
 `// C++ program to demonstrate the use of``// std::partial_sort``#include ``#include ``#include ``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;``}`

Output:

```1 1 3 10 3 3 7 7 8
```

Where can it be used ?

1. 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.
 `// C++ program to demonstrate the use of``// std::partial_sort``#include ``#include ``#include ``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;``}`

Output:

```The largest element is = 78
```
2. Finding the smallest element: Similar to finding the largest element, we can also find the smallest element in the container in the previous example.
 `// C++ program to demonstrate the use of``// std::partial_sort``#include ``#include ``#include ``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;``}`

Output:

```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.
 `// C++ program to demonstrate the use of``// std::partial_sort``#include ``#include ``#include ``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::sort(v1.begin(), v1.begin() + 2);`` ` `    ``cout << ``"v = "``;``    ``for` `(i = 0; i < 2; ++i) {``        ``cout << v[i] << ``" "``;``    ``}`` ` `    ``cout << ``"\nv1 = "``;``    ``for` `(i = 0; i < 2; ++i) {``        ``cout << v1[i] << ``" "``;``    ``}`` ` `    ``return` `0;``}`

Output:

```v = 3 10
v1 = 10 45
```

Explanation: Here, we applied std::partial_sort on v and std::sort on v1, upto second position. Now, you can understand that std::sort sorted only the element within the given range, whereas partial_sort took into consideration the whole container, but sorted only the first two positions.

This article is contributed by Mrigendra Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up