1 /* sort/test_heapsort.c
3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Thomas Walter, Brian Gough
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 int cmp_dbl (const void *a, const void *b);
21 void test_heapsort (size_t N);
22 void initialize (double *data, size_t N);
23 void cpy (double *dest, double *src, size_t N);
24 void randomize (double *data, size_t n);
25 void reverse (double *data, size_t N);
26 int check (double *data, double *orig, size_t N);
27 int pcheck (size_t * p, double *data, double *orig, size_t N);
30 test_heapsort (size_t N)
34 double *orig = (double *) malloc (N * sizeof (double));
35 double *data = (double *) malloc (N * sizeof (double));
36 size_t *p = (size_t *) malloc (N * sizeof(size_t));
43 status = gsl_heapsort_index (p, data, N, sizeof (double), (gsl_comparison_fn_t) & cmp_dbl);
44 status |= pcheck (p, data, orig, N);
45 gsl_test (status, "indexing array, n = %u, ordered", N);
47 gsl_heapsort (data, N, sizeof (double), (gsl_comparison_fn_t) & cmp_dbl);
48 status = check (data, orig, N);
50 gsl_test (status, "sorting, array, n = %u, ordered", N);
52 /* Reverse the data */
57 status = gsl_heapsort_index (p, data, N, sizeof (double), (gsl_comparison_fn_t) & cmp_dbl);
58 status |= pcheck (p, data, orig, N);
59 gsl_test (status, "indexing array, n = %u, reversed", N);
61 gsl_heapsort (data, N, sizeof (double), (gsl_comparison_fn_t) & cmp_dbl);
62 status = check (data, orig, N);
64 gsl_test (status, "sorting, array, n = %u, reversed", N);
66 /* Perform some shuffling */
71 status = gsl_heapsort_index (p, data, N, sizeof (double), (gsl_comparison_fn_t) & cmp_dbl);
72 status |= pcheck (p, data, orig, N);
73 gsl_test (status, "indexing array, n = %u, randomized", N);
75 gsl_heapsort (data, N, sizeof (double), (gsl_comparison_fn_t) & cmp_dbl);
76 status = check (data, orig, N);
78 gsl_test (status, "sorting, array, n = %u, randomized", N);
86 initialize (double *data, size_t N)
90 for (i = 0; i < N; i++)
97 cpy (double *dest, double *src, size_t N)
101 for (i = 0; i < N; i++)
108 randomize (double *data, size_t N)
112 for (i = 0; i < N; i++)
114 size_t j = urand (N);
115 double tmp = data[i];
122 reverse (double *data, size_t N)
126 for (i = 0; i < N / 2; i++)
128 size_t j = N - i - 1;
131 double tmp = data[i];
139 check (double *data, double *orig, size_t N)
143 for (i = 0; i < N; i++)
145 if (data[i] != orig[i])
155 pcheck (size_t * p, double *data, double *orig, size_t N)
159 for (i = 0; i < N; i++)
161 if (data[p[i]] != orig[i])
173 cmp_dbl (const void *a, const void *b)
175 const double x = *(const double *) a;
176 const double y = *(const double *) b;