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 (my, downheap) (BASE * data, const size_t stride, const size_t N, size_t k);
23 FUNCTION (my, downheap) (BASE * data, const size_t stride, const size_t N, size_t k)
25 BASE v = data[k * stride];
31 if (j < N && data[j * stride] < data[(j + 1) * stride])
36 if (!(v < data[j * stride])) /* avoid infinite loop if nan */
41 data[k * stride] = data[j * stride];
50 TYPE (gsl_sort) (BASE * data, const size_t stride, const size_t n)
57 return; /* No data to sort */
60 /* We have n_data elements, last element is at 'n_data-1', first at
61 '0' Set N to the last element number. */
66 k++; /* Compensate the first use of 'k--' */
70 FUNCTION (my, downheap) (data, stride, N, k);
76 /* first swap the elements */
77 BASE tmp = data[0 * stride];
78 data[0 * stride] = data[N * stride];
79 data[N * stride] = tmp;
81 /* then process the heap */
84 FUNCTION (my, downheap) (data, stride, N, 0);
89 TYPE (gsl_sort_vector) (TYPE (gsl_vector) * v)
91 TYPE (gsl_sort) (v->data, v->stride, v->size) ;