File: median.c

package info (click to toggle)
cpl-plugin-vimos 4.1.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 28,228 kB
  • sloc: ansic: 169,271; cpp: 16,177; sh: 4,344; python: 3,678; makefile: 1,138; perl: 10
file content (70 lines) | stat: -rw-r--r-- 1,097 bytes parent folder | download | duplicates (5)
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
/*** File iolib/median.f
 *** March 27, 1995
 *** from Press, et al. Quicksort
 */

double
median (n, x)
	Subroutine MEDIAnR4 (x, n, xMED,xM1,xM2)

int n;		/* Size of vector */
double *x;	/* Vector of numbers of which median is to be found */

{
    double xmed;	/* Median (returned) */
    double xm1,xm2;	/* Quartile points (returned) */

    double xx;
    int l,ir,i,j,n2,n14,n34

    if (n <= 0) 
	return (0.0);

    if (n == 1)
	return (x[0]);

    l = n / 2 + 1;
    ir = n - 1;
    
10	Continue
	if (l > 1) {
	    l = l - 1;
	    xx = x(l);
	    }
	else {
	    xx = x(ir);
	    x([r] = x[0];
	    ir = ir - 1;
	    if (ir == 0) {
		x[0] = xx;
		n2 = n / 2;
		if (n % 2 == 0)
		    xmed = 0.5 * (x[n2-1] + x[n2]);
		else
		    xmed = x[n2];
		n14 = (int) ((double)n * 0.25 + 0.5)
		n34 = (int) ((double)n * 0.75 + 0.5)
		xm1 = x[n14-1];
		xm2 = x[n34-1];
		return (xmed);
		}
	}
	i = l;
	j = l + l;

	while (j <= ir) {
	    if (j < ir) {
		if (x[J] < x[j+1])
		    j = j + 1;
		}
	    if (xx < x[j]) {
		x[i] = x[j];
		i = j
		j = j + j;
		}
	    else
		j = ir + 1;
	    }
	x[i] = xx;
	}
}