Template Function thrust::stable_sort(RandomAccessIterator, RandomAccessIterator, StrictWeakOrdering)¶
Function Documentation¶
-
template<typename
RandomAccessIterator
, typenameStrictWeakOrdering
>
voidthrust
::
stable_sort
(RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp) stable_sort
is much likesort:
it sorts the elements in[first, last)
into ascending order, meaning that ifi
andj
are any two valid iterators in[first, last)
such thati
precedesj
, then*j
is not less than*i
.As the name suggests,
stable_sort
is stable: it preserves the relative ordering of equivalent elements. That is, ifx
andy
are elements in[first, last)
such thatx
precedesy
, and if the two elements are equivalent (neitherx < y
nory < x
) then a postcondition ofstable_sort
is thatx
still precedesy
.This version of
stable_sort
compares objects using a function objectcomp
.The following code demonstrates how to sort integers in descending order using the greater<int> comparison operator.
- Parameters
first
: The beginning of the sequence.last
: The end of the sequence.comp
: Comparison operator.
- Template Parameters
RandomAccessIterator
: is a model of Random Access Iterator,RandomAccessIterator
is mutable, andRandomAccessIterator's
value_type
is convertible toStrictWeakOrdering's
first_argument_type
andsecond_argument_type
.StrictWeakOrdering
: is a model of Strict Weak Ordering.
#include <thrust/sort.h> #include <thrust/functional.h> ... const int N = 6; int A[N] = {1, 4, 2, 8, 5, 7}; thrust::sort(A, A + N, thrust::greater<int>()); // A is now {8, 7, 5, 4, 2, 1};
- See
- See
sort
- See
stable_sort_by_key