GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gsl/statistics/select_source.c at master · ampl/gsl

Expand file tree

/

select_source.c

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
112 lines (94 loc) · 3.19 KB
/

select_source.c

Copy path

File metadata and controls

112 lines (94 loc) · 3.19 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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);
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.