thrust::sort
Defined in thrust/sort.h
-
template<typename DerivedPolicy, typename RandomAccessIterator, typename StrictWeakOrdering>
void thrust::sort(const thrust::detail::execution_policy_base<DerivedPolicy> &exec, RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp) sortsorts the elements in[first, last)into ascending order, meaning that ifiandjare any two valid iterators in[first, last)such thatiprecedesj, then*jis not less than*i. Note:sortis not guaranteed to be stable. That is, suppose that*iand*jare equivalent: neither one is less than the other. It is not guaranteed that the relative order of these two elements will be preserved bysort.This version of
sortcompares objects using a function objectcomp.The algorithm’s execution is parallelized as determined by
exec.The following code demonstrates how to sort integers in descending order using the greater<int> comparison operator using the
thrust::hostexecution policy for parallelization:#include <thrust/sort.h> #include <thrust/functional.h> #include <thrust/execution_policy.h> ... const int N = 6; int A[N] = {1, 4, 2, 8, 5, 7}; thrust::sort(thrust::host, A, A + N, thrust::greater<int>()); // A is now {8, 7, 5, 4, 2, 1};
See also
stable_sortSee also
sort_by_key- Parameters
exec – The execution policy to use for parallelization.
first – The beginning of the sequence.
last – The end of the sequence.
comp – Comparison operator.
- Template Parameters
DerivedPolicy – The name of the derived execution policy.
RandomAccessIterator – is a model of Random Access Iterator,
RandomAccessIteratoris mutable, andRandomAccessIterator'svalue_typeis convertible toStrictWeakOrdering'sfirst argument type and second argument type.StrictWeakOrdering – is a model of Strict Weak Ordering.