GNU Scientific Library – Reference Manual: Sorting objects

Next: Sorting vectors, Up: Sorting [Index]

### 12.1 Sorting objects

The following function provides a simple alternative to the standard library function `qsort`. It is intended for systems lacking `qsort`, not as a replacement for it. The function `qsort` should be used whenever possible, as it will be faster and can provide stable ordering of equal elements. Documentation for `qsort` is available in the GNU C Library Reference Manual.

The functions described in this section are defined in the header file `gsl_heapsort.h`.

Function: void gsl_heapsort (void * array, size_t count, size_t size, gsl_comparison_fn_t compare)

  This function sorts the count elements of the array array, each of size size, into ascending order using the comparison function compare. The type of the comparison function is defined by,

      int (*gsl_comparison_fn_t) (const void * a,
                                  const void * b)

  A comparison function should return a negative integer if the first argument is less than the second argument, `0` if the two arguments are equal and a positive integer if the first argument is greater than the second argument.

  For example, the following function can be used to sort doubles into ascending numerical order.

      int
      compare_doubles (const double * a,
                       const double * b)
      {
          if (*a > *b)
             return 1;
          else if (*a < *b)
             return -1;
          else
             return 0;
      }

  The appropriate function call to perform the sort is,

      gsl_heapsort (array, count, sizeof(double),
                    compare_doubles);

  Note that unlike `qsort` the heapsort algorithm cannot be made into a stable sort by pointer arithmetic. The trick of comparing pointers for equal elements in the comparison function does not work for the heapsort algorithm. The heapsort algorithm performs an internal rearrangement of the data which destroys its initial ordering.

Function: int gsl_heapsort_index (size_t * p, const void * array, size_t count, size_t size, gsl_comparison_fn_t compare)

  This function indirectly sorts the count elements of the array array, each of size size, into ascending order using the comparison function compare. The resulting permutation is stored in p, an array of length n. The elements of p give the index of the array element which would have been stored in that position if the array had been sorted in place. The first element of p gives the index of the least element in array, and the last element of p gives the index of the greatest element in array. The array itself is not changed.

Next: Sorting vectors, Up: Sorting [Index]