2 * Implement Heap sort -- direct and indirect sorting
3 * Based on descriptions in Sedgewick "Algorithms in C"
5 * Copyright (C) 1999 Thomas Walter
7 * 18 February 2000: Modified for GSL by Brian Gough
9 * This is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 3, or (at your option) any
14 * This source is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 static inline void FUNCTION (index, downheap) (size_t * p, const BASE * data, const size_t stride, const size_t N, size_t k);
23 FUNCTION (index, downheap) (size_t * p, const BASE * data, const size_t stride, const size_t N, size_t k)
25 const size_t pki = p[k];
31 if (j < N && data[p[j] * stride] < data[p[j + 1] * stride])
36 if (!(data[pki * stride] < data[p[j] * stride])) /* avoid infinite loop if nan */
50 FUNCTION (gsl_sort, index) (size_t * p, const BASE * data, const size_t stride, const size_t n)
57 return; /* No data to sort */
60 /* set permutation to identity */
62 for (i = 0 ; i < n ; i++)
67 /* We have n_data elements, last element is at 'n_data-1', first at
68 '0' Set N to the last element number. */
73 k++; /* Compensate the first use of 'k--' */
77 FUNCTION (index, downheap) (p, data, stride, N, k);
83 /* first swap the elements */
88 /* then process the heap */
91 FUNCTION (index, downheap) (p, data, stride, N, 0);
96 FUNCTION (gsl_sort_vector, index) (gsl_permutation * permutation, const TYPE (gsl_vector) * v)
98 if (permutation->size != v->size)
100 GSL_ERROR ("permutation and vector lengths are not equal", GSL_EBADLEN);
103 FUNCTION (gsl_sort, index) (permutation->data, v->data, v->stride, v->size) ;