Function vector_sort_index (o2scl)

O2scl : Function List

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 in order, which give a sorted version of the vector. The value order[i] gives the index of entry in in data which corresponds to the ith value in the sorted vector. The vector data is unchanged by this function, and the initial values in order are ignored. Before calling this function, order must already be allocated as a vector of size n.

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 of data[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 and vec_size_t for which

  • vec_t has an operator[], and

  • vec_size_t has an operator[] which returns a size_t . One possible type for vec_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 in order, which give a sorted version of the vector. The value order[i] gives the index of entry in in data which corresponds to the ith value in the sorted vector. The vector data is unchanged by this function, and the initial values in order are ignored. Before calling this function, order must already be allocated as a vector of size n.

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 of data[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 and vec_size_t for which

  • vec_t has an operator[], and

  • vec_size_t has an operator[] which returns a size_t . One possible type for vec_size_t is o2scl::permutation.

This works similarly to the GSL function gsl_sort_index().