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
1
vote
3 answers

How to call stl::nth_element with a member function inside this class?

I want to use the function nth_element with my own sorting function (which should have access to the data of the object) within a class. Currently, I am doing the following: class Foo { public: glm::vec3 *points; int nmbPoints; bool…
DanceIgel
  • 646
  • 7
  • 24
1
vote
1 answer

Medians of upper and lower halves of a vector

I am trying to compile an Octave .oct function to calculate the medians of the upper and lower "halves" of a sorted vector which will vary in length e.g. for an odd length vector such as [5,8,4,6,7] I want the "lower" median value of 4,5 and 6 and…
Andrew
  • 51
  • 1
  • 4
1
vote
1 answer

Partial sorting: nth elements having preseved order

The task is to partially sort a vector with duplicates s.t. the median (nth-element) is at the position it would be, if the vector were sorted. All smaller elements should be on the left, all larger elements on the right. All elements with value…
aces
  • 165
  • 1
  • 8
1
vote
2 answers

Are duplicates of the nth element always contiguous when using std::nth_element?

vector data = {3, 1, 5, 3, 3, 8, 7, 3, 2}; std::nth_element(data.begin(), data.begin() + median, data.end()); Will this always result in: data = {less, less, 3, 3, 3, 3, larger, larger, larger} ? Or would a other possible outcome be: data =…
aces
  • 165
  • 1
  • 8
1
vote
2 answers

Efficient median computation

I have an array, A, of length n. Let B be an array (that we never want to store separately - this is just to help explain) containing every k'th element of A. I want to find the median of B, and I want to move that element of A to the floor(n/2)'th…
PThomasCS
  • 1,160
  • 2
  • 10
  • 17
1
vote
1 answer

Strange behavior with std::nth_element

I'm trying to find the median of vectors of (x,y) points using nth_element cv::Point2f medOffset; vector tempOffsetsX = offsetsX; int medLoc = tempOffsetsX.size()/2; nth_element(tempOffsetsX.begin(),…
ryan0270
  • 1,095
  • 9
  • 29
0
votes
2 answers

How to find nth element of list and have answer be in R PROLOG

Very new to prolog. I am trying to create a simple recursive rule to find nth element on the list. For example if I have a list of strings or numbers I want to be able to use the query ?- findme([dog, cat , bird], 1, R). R = dog ?-…
0
votes
0 answers

Select Nth value of a column in access database

I'm trying to select specific values in an Access database. So the database contains many colums, I'm only able to select the first values, or last: SELECT Column1 , First(Column7) AS Column7_1 , First(Column8) AS FirstOfColumn8 ,…
0
votes
2 answers

Select Nth value in access database (SQL)

I'm trying to select specific values in an Access database. So the database contains many colums, I'm only able to select the first values, or last: SELECT Column1, First(Column7) AS Column7_1, First(Column8) AS FirstOfColumn8, [Column1] &…
0
votes
2 answers

Error: nth_element- No instance of overloaded function (Median Finder Program)

I am so close to finishing this program. It will find the median of an array of 5 values. I have one last error that I cannot seem to get to go away. Since I am new to C++, I have no idea of what the problem could be. I researched the error over and…
1
2