Function vector_sort_index (o2scl)¶
-
template<class vec_t, class vec_size_t>
void o2scl::vector_sort_index(size_t n, const vec_t &data, vec_size_t &order)¶ Create a permutation which sorts the first
n
elements of a vector (in increasing order)This function takes a vector
data
and arranges a list of indices inorder
, which give a sorted version of the vector. The valueorder[i]
gives the index of entry in indata
which corresponds to thei
th value in the sorted vector. The vectordata
is unchanged by this function, and the initial values inorder
are ignored. Before calling this function,order
must already be allocated as a vector of sizen
.For example, after calling this function, a sorted version the vector can be output with
size_t n=5; double data[5]={3.1,4.1,5.9,2.6,3.5}; permutation order(n); vector_sort_index(n,data,order); for(size_t i=0;i<n;i++) { cout << data[order[i]] << endl; }
To create a permutation which stores as its
i
th element, the index ofdata[i]
in the sorted vector, you can invert the permutation created by this function.This is a generic sorting template function. It will work for any types
vec_t
andvec_size_t
for whichvec_t
has anoperator[]
, andvec_size_t
has anoperator[]
which returns asize_t
. One possible type forvec_size_t
is o2scl::permutation.
This works similarly to the GSL function
gsl_sort_index()
.
-
template<class vec_t, class vec_size_t>
void o2scl::vector_sort_index(const vec_t &data, vec_size_t &order)¶ Create a permutation which sorts a vector (in increasing order)
This function takes a vector
data
and arranges a list of indices inorder
, which give a sorted version of the vector. The valueorder[i]
gives the index of entry in indata
which corresponds to thei
th value in the sorted vector. The vectordata
is unchanged by this function, and the initial values inorder
are ignored. Before calling this function,order
must already be allocated as a vector of sizen
.For example, after calling this function, a sorted version the vector can be output with
size_t n=5; double data[5]={3.1,4.1,5.9,2.6,3.5}; permutation order(n); vector_sort_index(n,data,order); for(size_t i=0;i<n;i++) { cout << data[order[i]] << endl; }
To create a permutation which stores as its
i
th element, the index ofdata[i]
in the sorted vector, you can invert the permutation created by this function.This is a generic sorting template function. It will work for any types
vec_t
andvec_size_t
for whichvec_t
has anoperator[]
, andvec_size_t
has anoperator[]
which returns asize_t
. One possible type forvec_size_t
is o2scl::permutation.
This works similarly to the GSL function
gsl_sort_index()
.