Questions tagged [nth-element]

Standard Template Library (STL) function that partially orders the array, ensuring that the nth element is in its rightful place as it would be if the array were sorted, also lower elements placed in lower positions, and vice versa for higher elements.

nth_element is a C++ Standard Template Library (STL) algorithm that, like the name suggests, places the nth element of an array into the place it would go in that array if it were sorted. It also guarantees that all lower positions have elements less than the nth element and all higher positions have elements greater than the nth position.

For more information on this function, see: http://www.sgi.com/tech/stl/nth_element.html

When a single rank order statistic is needed, it is preferable to use this function over std::sort since std::nth_elements's implementation is guaranteed to be O(n) in the average case.

Notice that this function can be used to find the median of a collection of elements and rearrange them as described above.

25 questions
28
votes
3 answers

How is nth_element Implemented?

There are a lot of claims on StackOverflow and elsewhere that nth_element is O(n) and that it is typically implemented with Introselect: http://en.cppreference.com/w/cpp/algorithm/nth_element I want to know how this can be achieved. I looked at…
Jonathan Mee
  • 35,107
  • 16
  • 95
  • 241
20
votes
3 answers

nth_element implementations complexities

Does anyone know both the expected running times and worst case running times for different implementations of std::nth_element? I use this algorithm nearly every day. I'm specifically interested in the STL versions shipping with the recent…
Chris A.
  • 6,379
  • 1
  • 23
  • 41
19
votes
2 answers

algorithm for nth_element

I have recently found out that there exists a method called nth_element in the STL. To quote the description: Nth_element is similar to partial_sort, in that it partially orders a range of elements: it arranges the range [first, last) such …
martinus
  • 16,710
  • 15
  • 65
  • 90
16
votes
3 answers

Calling std::nth_element() function extremely frequently

I did not find this specific topic anywhere... I am calling the nth_element() algorithm about 400,000 times per second on different data in a std::vector of 23 integers, more precise "unsigned short" values. I want to improve computation speed and…
karsten
  • 573
  • 3
  • 8
13
votes
3 answers

Does C# have a std::nth_element equivalent?

I'm porting some C++ code to C#. Does C# have an equivalent to std::nth_element() or do I need to roll my own?
Drew Dormann
  • 50,103
  • 11
  • 109
  • 162
12
votes
7 answers

finding quartiles

I've written a program where the user can enter any number of values into a vector and it's supposed to return the quartiles, but I keep getting a "vector subscript out of range" error : #include "stdafx.h" #include #include…
Emir
  • 471
  • 1
  • 5
  • 15
9
votes
1 answer

Why does std::nth_element return sorted vectors for input vectors with N < 33 elements?

I am using std::nth_element to get a (roughly correct) value for a percentile of a vector, like so: double percentile(std::vector &vectorIn, double percent) { std::nth_element(vectorIn.begin(), vectorIn.begin() +…
stack_horst
  • 292
  • 2
  • 10
5
votes
1 answer

What's the equivalent 'nth_element' function in Python?

I want to implement Vantage Point Tree in the python, but it use the std::nth_element in the C++. So I want to find the equivalent 'nth_element' function in Python or in numpy. Notice, the nth_element would only partial order an array, and it's…
Colin Ji
  • 773
  • 6
  • 15
4
votes
2 answers

Towards understanding nth_element

I am having a difficult time grasping how I should use std::nth_element, since I am little rusty. The ref says: Rearranges the elements in the range [first,last), in such a way that the element at the nth position is the element that would be in…
gsamaras
  • 66,800
  • 33
  • 152
  • 256
2
votes
1 answer

Is there a way to do nth_element together with copy of data?

I wish to compute the median value from an array of floats in C++: float Median( FloatArray const * constFloatArray ) { FloatArray scratch = FloatArray( *constFloatArray ); int64_t const size = scratch.GetWidth() *…
WilliamKF
  • 36,283
  • 61
  • 170
  • 271
2
votes
2 answers

std::nth_element provides wrong value

From a given unsorted vector I want to get the nth smallest element. I figured out that there is a method in the standard library. But I do not understand the following result. I took the vector with entries {3,4,5,2,3} and want to have the 2th…
DerJFK
  • 221
  • 2
  • 12
1
vote
1 answer

Using std::nth_element to sort rows of an arma::mat

As the title mentions my goal is to use std::nth_element on a Armadillo-Matrix which contains a set of N points in d dimensions, i.e. a Nxd matrix. The rows represent a d-dimensional point. The comparison of two rows should be done along a fixed…
plumbum452
  • 11
  • 3
1
vote
1 answer

nth_element implementation based on modified quick_sort, not working as expected

For the purpose of understanding quick_sort I am trying to implement nth_element. As ground truth for testing I am using std::nth_element My algorithm fails on many inputs, for ex on a = {6,1,7,5,3,8,2,4,9} How to fix it? std::random_device…
spin_eight
  • 3,747
  • 10
  • 33
  • 58
1
vote
2 answers

Quickselect algorithm for singly linked list C++

I need an algorithm which can find the median of a singly linked list in linear time complexity O(n) and constant space complexity O(1). EDIT: The singly linked list is a C-style singly linked list. No stl allowed (no container, no functions,…
1
vote
2 answers

Finding nth largest element inplace

I need to find nth largest element in an array and currently I'm doing it the following way: std::vector buffer(sequence); // sequence is const std::vector std::nth_element(buffer.begin(), buffer.begin() + idx, buffer.end(),…
aob
  • 197
  • 6
1
2