GNU Astronomy Utilities


Next: , Previous: , Up: Gnuastro library   [Contents][Index]


10.3.16 Qsort functions (qsort.h)

The C programming language comes with the qsort (Quick sort) function. qsort is a generic function which allows you to sort any kind of data structure (not just a single number). To to define greater and smaller (for sorting), qsort needs another function, even for simple numerical types. To facilitate numerical sorting for Gnuastro’s programs/libraries, Gnuastro defines a function for each type’s increasing and decreasing function. You can pass these functions to qsort when your array has the respective type (see Numeric data types).

Global variable: gal_qsort_index_arr

Pointer to a floating point array (float *) to use as a reference in gal_qsort_index_float_decreasing, see the explanation there for more.

Function:
int
gal_qsort_index_float_decreasing (const void *a, const void *b)

When passed to qsort, this function will sort a size_t array based on decreasing values in the gal_qsort_index_arr single precision floating point array. The floating point array will not be changed, it is only read. For example, if we have the following source code:

#include <stdio.h>
#include <stdlib.h>           /* qsort is defined in stdlib.h. */
#include <gnuastro/qsort.h>

int
main (void)
{
  size_t s[4]={0, 1, 2, 3};
  float f[4]={1.3,0.2,1.8,0.1};
  gal_qsort_index_arr=f;
  qsort(s, 4, sizeof(size_t), gal_qsort_index_float_decreasing);
  printf("%zu, %zu, %zu, %zu\n", s[0], s[1], s[2], s[3]);
  return EXIT_SUCCESS;
}

The output will be: 2, 0, 1, 3.

Function:
int
gal_qsort_TYPE_increasing (const void *a, const void *b)

When passed to qsort, this function will sort an TYPE array in increasing order (first element will be the smallest). Please replace TYPE (in the function name) with one of the Numeric data types, for example gal_qsort_int32_increasing, or gal_qsort_float64_increasing.

Function:
int
gal_qsort_TYPE_decreasing (const void *a, const void *b)

When passed to qsort, this function will sort an TYPE array in decreasing order (first element will be the largest). Please replace TYPE (in the function name) with one of the Numeric data types, for example gal_qsort_int32_decreasing, or gal_qsort_float64_decreasing.


Next: , Previous: , Up: Gnuastro library   [Contents][Index]