public void nth_element_3()
{
// Example from http://www.tenouk.com/cpluscodesnippet/cplusstlvectoralgorithmnth_element.html
double[] a = { 0, 1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15, 20, 21, 22, 23, 24, 25 };
Sort.NthElement(a, 0, a.Length, 3);
double[] expected = { 0, 1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15, 20, 21, 22, 23, 24, 25 };
Assert.IsTrue(a.IsEqual(expected));
Func<double, double, int> greater = (double elem1, double elem2) =>
{
// Less than zero: This instance is less than value.
// Zero: This instance is equal to value.
// Greater than zero: This instance is greater than value.
int ret = -elem1.CompareTo(elem2);
return ret;
};
Sort.NthElement(a, 0, a.Length, 4, greater);
expected = new double[] { 25, 24, 23, 22, 21, 20, 15, 14, 13, 12, 11, 10, 5, 4, 3, 2, 1, 0 };
Assert.IsTrue(a.IsEqual(expected));
Vector.Shuffle(a);
Sort.NthElement(a, 0, a.Length, 4, greater);
expected = new double[] { 25, 24, 23, 22, 21, 20, 15, 14, 13, 12, 11, 10, 5, 4, 3, 2, 1, 0 };
Assert.IsTrue(a.IsEqual(expected));
}