File: select_source.c

package info (click to toggle)
gsl 2.6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 26,176 kB
  • sloc: ansic: 255,377; sh: 11,435; makefile: 903; python: 68
file content (112 lines) | stat: -rw-r--r-- 3,269 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* statistics/select_source.c
 * 
 * Copyright (C) 2018 Patrick Alken
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#define SWAP(a,b) do { tmp = b ; b = a ; a = tmp ; } while(0)

/*
gsl_stats_select()
  Select k-th largest element from an unsorted array using
quickselect algorithm

Inputs: data   - unsorted array containing the observations
        stride - stride
        n      - length of 'data'
        k      - desired element in [0,n-1]

Return: k-th largest element of data[]
*/

BASE
FUNCTION(gsl_stats,select) (BASE data[],
                            const size_t stride,
                            const size_t n,
                            const size_t k)
{
  if (n == 0)
    {
      GSL_ERROR_VAL("array size must be positive", GSL_EBADLEN, 0.0);
    }
  else
    {
      size_t left = 0;
      size_t right = n - 1;
      size_t mid, i, j;
      BASE pivot, tmp;

      while (1)
        {
          if (right <= left + 1)
            {
              if (right == left + 1 && data[right * stride] < data[left * stride])
                {
                  SWAP(data[left * stride], data[right * stride]);
                }

              return data[k * stride];
            }
          else
            {
              mid = (left + right) >> 1;
              SWAP(data[mid * stride], data[(left + 1) * stride]);

              if (data[left * stride] > data[right * stride])
                {
                  SWAP(data[left * stride], data[right * stride]);
                }

              if (data[(left + 1) * stride] > data[right * stride])
                {
                  SWAP(data[(left + 1) * stride], data[right * stride]);
                }

              if (data[left * stride] > data[(left + 1) * stride])
                {
                  SWAP(data[left * stride], data[(left + 1) * stride]);
                }

              i = left + 1;
              j = right;
              pivot = data[(left + 1) * stride];

              while (1)
                {
                  do i++; while (data[i * stride] < pivot);
                  do j--; while (data[j * stride] > pivot);

                  if (j < i)
                    break;

                  SWAP(data[i * stride], data[j * stride]);
                }

              data[(left + 1) * stride] = data[j * stride];
              data[j * stride] = pivot;

              if (j >= k)
                right = j - 1;

              if (j <= k)
                left = i;
            }
        }

      /* will never get here */
      GSL_ERROR_VAL("select error", GSL_FAILURE, 0.0);
    }
}