/* nag_double_sort (m01cac) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 1, 1990.
 * Mark 8 revised, 2004.
 */

#include <nag.h>
#include <stdio.h>
#include <nag_stdlib.h>
#include <nag_stddef.h>
#include <nagm01.h>

int main(void)
{
  Integer  exit_status = 0, i, n;
  NagError fail;
  double   *vec = 0;

  INIT_FAIL(fail);


  /* Skip heading in data file */
  scanf("%*[^\n]");
  printf("nag_double_sort (m01cac) Example Program Results\n");
  scanf("%ld", &n);
  if (n >= 1)
    {
      if (!(vec = NAG_ALLOC(n, double)))
        {
          printf("Allocation failure\n");
          exit_status = -1;
          goto END;
        }
    }
  else
    {
      printf("Invalid n.\n");
      exit_status = 1;
      return exit_status;
    }
  for (i = 0; i < n; ++i)
    scanf("%lf", &vec[i]);
  /* nag_double_sort (m01cac).
   * Quicksort of set of values of data type double
   */
  nag_double_sort(vec, (size_t) n, Nag_Ascending, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_double_sort (m01cac).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }
  printf("Sorted numbers\n\n");
  for (i = 0; i < n; ++i)
    printf("%10.6g%c", vec[i], (i%7 == 6 || i == n-1)?'\n':' ');
 END:
  NAG_FREE(vec);
  return exit_status;
}