std::nth_element() is an STL algorithm that rearranges the list in such a way such that the element at the nth position is the one which should be at that position if we sort the list.
It does not sort the list, just that all the elements, which precede the nth element are not greater than it, and all the elements which succeed it are not less than it.
It has two versions, which are defined below:
1. Comparing elements using “<“:
Syntax
void nth_element (RandomAccessIterator first, RandomAccessIterator nth,
RandomAccessIterator last);
Parameters
- first: Random-access iterator to the first element in the list.
- last: Random-access iterator to the last element in the list.
- nth: Random-access iterator pointing to the position in the list, which should be sorted.
If it points to the end, then this function will do nothing.
Return Value
- Since the return type is void, so it does not return any value.
Example
CPP
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int v[] = { 3, 2, 10, 45, 33, 56, 23, 47 }, i;
std::nth_element(v, v + 4, v + 8);
for (i = 0; i < 8; ++i) {
cout << v[i] << " " ;
}
return 0;
}
|
Output
3 2 10 23 33 56 45 47
Here, the fifth element is 33, and all elements to its left are smaller than it and all elements to its right are greater than it.
2. Comparing using a User-Defined Function
Syntax
void nth_element (RandomAccessIterator first, RandomAccessIterator nth,
RandomAccessIterator last, Compare comp);
Here, the first, last, and nth arguments are the same as in the 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 the 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
- Since its return type is void, it does not return any value.
Example
CPP
#include <algorithm>
#include <iostream>
using namespace std;
bool comp( int a, int b) { return (a < b); }
int main()
{
int v[] = { 3, 2, 10, 45, 33, 56, 23, 47 }, i;
std::nth_element(v, v + 5, v + 8, comp);
for (i = 0; i < 8; ++i) {
cout << v[i] << " " ;
}
return 0;
}
|
Output
33 2 10 23 3 45 47 56
In this code, since the nth element as pointed out by the second argument in std::nth_element is the sixth element of the array v, so this means that the sixth element in the array after the application of std::nth_element should be the one that would have been there if the whole array was sorted, i.e., 45. And also all the elements to its left are either less than it or equal to it and the elements on its right are greater than it. Purpose of Binary Function comp: std::nth_element partially sorts the range [first, last) in ascending order so that the condition, *i < *j, (for version 1 ), or comp(*i, *j) == true (for version 2) is met for any i in the range [first, nth) and for any j in the range [nth, last). So, comp() is used to ensure that all the elements before the nth_element are less than elements after the nth_element.
Where can we apply std::nth_element()?
1. It can be used if we want to find the first n smallest numbers, but they may or maynot be ordered.
CPP
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int v[] = { 30, 20, 10, 40, 60, 50, 70, 80 }, i;
std::nth_element(v, v + 2, v + 8);
for (i = 0; i < 3; ++i) {
cout << v[i] << " " ;
}
return 0;
}
|
2. Just like first n smallest number, we can also find first n largest numbers, by just changing the Binary Function passed as argument in std::nth_element.
CPP
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int v[] = { 30, 20, 50, 60, 70, 10, 80, 40 }, i;
std::nth_element(v, v + 1, v + 8, std::greater< int >());
for (i = 0; i < 2; ++i) {
cout << v[i] << " " ;
}
return 0;
}
|
Here, we have passed greater() as binary function, so now nth element will be the one which should be at the nth place if we sort the given array in descending order, so first n elements will be the first n largest elements.
3. It can be used to find the median of the elements given.
CPP
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< int > v = { 3, 2, 10, 45, 33, 56, 23, 47, 60 }, i;
std::nth_element(v.begin(), v.begin() + v.size() / 2,
v.end());
cout << "The median of the array is "
<< v[v.size() / 2];
return 0;
}
|
Output
The median of the array is 33
Here the sorted array will be 2 3 10 23 33 45 47 56 60, so there are 9 elements and the median will be the middle element, i.e., 5th element: 33.
Time Complexity of std::nth_element(): O(n), with n being the distance between the first and the last.
Internal Implementation of std::nth_element
The std::nth_element algorithm is implemented using a hybrid approach called “Introselect.” This technique combines the efficiency of both Quickselect and the Median of Medians algorithm:
- Quickselect:
- Quickselect is employed to find the kth smallest element in an unsorted array.
- It is a selection algorithm based on the partitioning step of the Quick Sort algorithm.
- Quickselect focuses on a specific partition containing the kth element, avoiding unnecessary sorting of other elements.
- Median of Medians:
- The Median of Medians algorithm is used to enhance the selection of a pivot element during partitioning.
- It is primarily used in Quickselect to improve pivot selection, which affects the algorithm’s performance.
- The Median of Medians algorithm ensures a more balanced partitioning and reduces the likelihood of worst-case scenarios.
Related Articles:
Similar Reads
std::min_element in C++
The std::min_element() in C++ is an STL algorithm that is used to find the minimum element in a given range. This range can be array, vector, list or any other container. It is defined inside the <algorithm> header file. In this article, we will learn about the std::min_element() in C++. Examp
4 min read
std::generate_n in C++
std::generate is an STL algorithm, which is used to generate numbers based upon a generator function, and then, it assigns those values to the elements in the container in the range [first, last). The generator function has to be defined by the user, and it is called successively for assigning the n
2 min read
std::find_end in C++
std::find_end is used to find the last occurrence of a sub-sequence inside a container. It searches the range [first1,last1) for the last occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found. It is similar to std::se
6 min read
Delete elements in C++ STL list
How to insert elements in C++ STL List ? This article covers the deletion aspects in STL list. Using list::erase(): The purpose of this function is to remove the elements from list. Single or multiple contiguous elements in range can be removed using this function. This function takes 2 arguments, s
4 min read
std::next in C++
std::next returns an iterator pointing to the element after being advanced by certain no. of positions. It is defined inside the header file . It does not modify its arguments and returns a copy of the argument advanced by the specified amount. If it is a random-access iterator, the function uses ju
4 min read
std::generate in C++
std::generate, as the name suggests is an STL algorithm, which is used to generate numbers based upon a generator function, and then, it assigns those values to the elements in the container in the range [first, last). The generator function has to be defined by the user, and it is called successive
2 min read
std::front_inserter in C++
std::front_inserter constructs a front-insert iterator that inserts new elements at the front of the container to which it is applied. It is defined inside the header file . A front-insert iterator is a special type of output iterator designed to allow algorithms that usually overwrite elements (suc
4 min read
How to insert elements in C++ STL List ?
List has been discussed in many articles, but the sole purpose of this article is to cover all types of insertions that are possible to be carried in a list container and to give a detailed insight on the insertion operations. List and its many functions are defined under the header file "list" . Va
6 min read
std::count() in C++ STL
In C++, the count() is a built-in function used to find the number of occurrences of an element in the given range. This range can be any STL container or an array. In this article, we will learn about the count() function in C++. Letâs take a quick look at a simple example that uses count() method:
3 min read
Get Map Element at Offset in C++ STL
Prerequisites: Map in C++ STL Since the map is not indexed as arrays or vectors sequential access is not possible in the map but to access an element at a particular offset. Example: inserted elements are { 'a',11 } , { 'b',12 } , { ' c ', 13 } then we can get { 'b', 12 } for 2 position. Methods to
3 min read