NAG Library Manual, Mark 30.2
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_sort_quicksort (m01csc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.2, 2024.
 */

#include <nag.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif
static Integer NAG_CALL compare(const Nag_Pointer a, const Nag_Pointer b);
#ifdef __cplusplus
}
#endif

#define VEC(I, J) vec[(I)*tdvec + J]
int main(void) {
  Integer exit_status = 0, i, j, k, m, n, tdvec;
  double *vec = 0;
  NagError fail;

  INIT_FAIL(fail);

  /* Skip heading in data file */
  scanf("%*[^\n]");
  printf("nag_sort_quicksort (m01csc) Example Program Results\n");
  scanf("%" NAG_IFMT "", &m);
  scanf("%" NAG_IFMT "", &n);
  scanf("%" NAG_IFMT "", &k);
  if (m >= 1 && n >= 1 && k >= 1) {
    if (!(vec = NAG_ALLOC(m * n, double))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
    tdvec = n;
  } else {
    printf("Invalid m or n or k.\n");
    exit_status = 1;
    return exit_status;
  }
  for (i = 0; i < m; ++i)
    for (j = 0; j < n; ++j)
      scanf("%lf", &VEC(i, j));
  /* nag_sort_quicksort (m01csc).
   * Quicksort of set of values of arbitrary data type
   */
  nag_sort_quicksort((Pointer)&VEC(0, k - 1), (size_t)m, sizeof(double),
                     (ptrdiff_t)(sizeof(double) * n), compare, Nag_Ascending,
                     &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sort_quicksort (m01csc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  printf("\nMatrix with column %" NAG_IFMT " sorted \n", k);
  for (i = 0; i < m; ++i) {
    for (j = 0; j < n; ++j)
      printf(" %8.1f ", VEC(i, j));
    printf("\n");
  }

END:
  NAG_FREE(vec);

  return exit_status;
}

static Integer NAG_CALL compare(const Nag_Pointer a, const Nag_Pointer b) {
  double x = *((const double *)a);
  double y = *((const double *)b);
  return (x < y ? -1 : (x == y ? 0 : 1));
}