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
nelements of a vector (in increasing order)This function takes a vector
dataand arranges a list of indices inorder, which give a sorted version of the vector. The valueorder[i]gives the index of entry in indatawhich corresponds to theith value in the sorted vector. The vectordatais unchanged by this function, and the initial values inorderare ignored. Before calling this function,ordermust 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
ith 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_tandvec_size_tfor whichvec_thas anoperator[], andvec_size_thas anoperator[]which returns asize_t. One possible type forvec_size_tis 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
dataand arranges a list of indices inorder, which give a sorted version of the vector. The valueorder[i]gives the index of entry in indatawhich corresponds to theith value in the sorted vector. The vectordatais unchanged by this function, and the initial values inorderare ignored. Before calling this function,ordermust 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
ith 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_tandvec_size_tfor whichvec_thas anoperator[], andvec_size_thas anoperator[]which returns asize_t. One possible type forvec_size_tis o2scl::permutation.
This works similarly to the GSL function
gsl_sort_index().