partial_sort() function in C++ Algorithm
In this tutorial, we will learn how to perform the partial_sort() function in C++ with the corresponding code.
The partial_sort() function is same as the sort() function but the difference is sort() function is used for sorting entire range elements unlikely partial_sort() is used to sort subpart of it.
std::partial_sort:
It sorts the elements in the range [first, last). The elements from first are sorted in ascending order till the middle element. The remaining elements will be arranged randomly without any order .
The elements are compared using two methods:
- Operator ‘<‘ for the first
- comp for the second
Sorting elements using the partial_sort() function.
1. comparing elements using <:
Syntax:
void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);Parameter:
The first is a Random-Access iterator to the first element.
The last is a Random-Access iterator to the last element.
The middle is a Random-Access iterator, which is used to point the element in the range [first, last). It is used as the upper boundary to the elements which are fully sorted.
Below program is the example for Partial_sort() function using comparison operator “<“.
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v = {5,8,1,3,6,9,7,2,4};
vector<int>::iterator itr;
//displaying before sorting
cout<<"Before sorting: ";
for (itr = v.begin(); itr != v.end(); ++itr)
{
cout << *itr << " ";
}
// using default comparison (operator <):
partial_sort(v.begin(), v.begin() + 4, v.end());
// displaying after sorting
cout<<"\nAfter sorting: ";
for (itr = v.begin(); itr != v.end(); ++itr)
{
cout << *itr << " ";
}
return 0;
}output:
Before sorting: 5 8 1 3 6 9 7 2 4 After sorting: 1 2 3 4 8 9 7 6 5
2. comparing using a function:
syntax:
void partial_sort (RandomAccessIterator first, RandomAccessIterator middle,RandomAccessIterator last, Compare comp);parameter:
The comp is a comparison function object which takes two arguments. It returns true when it is satisfied and false when it is not satisfied.
Let us look into an example for partial_sort() function using the comp.
#include <bits/stdc++.h>
using namespace std;
bool myfunction (int x,int y)
{
return (x<y);
}
int main ()
{
int a[] = {10,5,8,1,3,6,9,7,2,4};
vector<int> v (a, a+10);
//displaying before sorting
cout<<"before sorting:";
for (vector<int>::iterator itr=v.begin(); itr!=v.end(); ++itr)
cout << ' ' << *itr;
cout << '\n';
partial_sort (v.begin(), v.begin()+4, v.end(),myfunction);
//displaying after sorting
cout << "after sorting:";
for (vector<int>::iterator itr=v.begin(); itr!=v.end(); ++itr)
cout << ' ' << *itr;
cout << '\n';
return 0;
}Output:
before sorting: 10 5 8 1 3 6 9 7 2 4 after sorting: 1 2 3 4 10 8 9 7 6 5
Return value: none, because it has a void return type.
Exceptions:
It throws an exception if any of the comparisons, swaps, or the operations on iterators throws.
Note:If there are any invalid parameters then it causes undefined behavior.
Thus, we hope this tutorial helped you to use perform partial_sort() function in C++.
You may also visit:
- C++ program to add two polynomial using a linked list
- Find the number of squares inside a given square grid in C++
Thank You.
Leave a Reply