1 /* sort/subsetind_source.c
3 * Copyright (C) 1999,2000,2001 Thomas Walter, Brian Gough
5 * This is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 3, or (at your option) any
10 * This source is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 /* find the k-th smallest elements of the vector data, in ascending order */
19 FUNCTION (gsl_sort, smallest_index) (size_t * p, const size_t k,
20 const BASE * src, const size_t stride,
28 GSL_ERROR ("subset length k exceeds vector length n", GSL_EINVAL);
36 /* take the first element */
39 xbound = src[0 * stride];
42 /* examine the remaining elements */
44 for (i = 1; i < n; i++)
48 BASE xi = src[i * stride];
54 else if (xi >= xbound)
59 for (i1 = j - 1; i1 > 0 ; i1--)
61 if (xi > src[p[i1 - 1] * stride])
69 xbound = src[p[j-1] * stride];
77 FUNCTION (gsl_sort_vector,smallest_index) (size_t * p, const size_t k,
78 const TYPE (gsl_vector) * v)
80 return FUNCTION (gsl_sort, smallest_index) (p, k, v->data, v->stride, v->size);
84 FUNCTION (gsl_sort, largest_index) (size_t * p, const size_t k,
85 const BASE * src, const size_t stride,
93 GSL_ERROR ("subset length k exceeds vector length n", GSL_EINVAL);
101 /* take the first element */
104 xbound = src[0 * stride];
107 /* examine the remaining elements */
109 for (i = 1; i < n; i++)
113 BASE xi = src[i * stride];
119 else if (xi <= xbound)
124 for (i1 = j - 1; i1 > 0 ; i1--)
126 if (xi < src[stride * p[i1 - 1]])
134 xbound = src[stride * p[j-1]];
142 FUNCTION (gsl_sort_vector,largest_index) (size_t * p, const size_t k,
143 const TYPE (gsl_vector) * v)
145 return FUNCTION (gsl_sort, largest_index) (p, k, v->data, v->stride, v->size);