## File: Sorting-Examples.html

package info (click to toggle)
gsl-ref-html 2.3-1
• area: non-free
• in suites: bullseye, buster, sid
• size: 6,876 kB
• ctags: 4,574
• sloc: makefile: 35
 file content (146 lines) | stat: -rw-r--r-- 5,195 bytes parent folder | download
 `123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146` `````` GNU Scientific Library – Reference Manual: Sorting Examples

12.5 Examples

The following example shows how to use the permutation p to print the elements of the vector v in ascending order,

gsl_sort_vector_index (p, v);  for (i = 0; i < v->size; i++) {     double vpi = gsl_vector_get (v, p->data[i]);     printf ("order = %d, value = %g\n", i, vpi); }

The next example uses the function gsl_sort_smallest to select the 5 smallest numbers from 100000 uniform random variates stored in an array,

#include <gsl/gsl_rng.h> #include <gsl/gsl_sort_double.h>  int main (void) {   const gsl_rng_type * T;   gsl_rng * r;    size_t i, k = 5, N = 100000;    double * x = malloc (N * sizeof(double));   double * small = malloc (k * sizeof(double));    gsl_rng_env_setup();    T = gsl_rng_default;   r = gsl_rng_alloc (T);    for (i = 0; i < N; i++)     {       x[i] = gsl_rng_uniform(r);     }    gsl_sort_smallest (small, k, x, 1, N);    printf ("%zu smallest values from %zu\n", k, N);    for (i = 0; i < k; i++)     {       printf ("%zu: %.18f\n", i, small[i]);     }    free (x);   free (small);   gsl_rng_free (r);   return 0; }

The output lists the 5 smallest values, in ascending order,

\$ ./a.out
5 smallest values from 100000 0: 0.000003489200025797 1: 0.000008199829608202 2: 0.000008953968062997 3: 0.000010712770745158 4: 0.000033531803637743
``````