6 int median(int *a
, unsigned int n
)
9 int i
, j
; /* working bottom, top */
10 int bottom
= 0; /* window bottom */
11 int top
= n
- 1; /*window top */
13 if (! (n
& 1)){ /*return lower of 2 centre values */
17 while (bottom
< top
) {
21 while (a
[i
] < a
[middle
]){
24 while (a
[middle
] < a
[j
]){
46 int sort_median(int *a
, int n
)
49 /*stupid sort, but n is very small*/
50 for (i
= 0; i
< n
; i
++){
51 for (j
= i
+ 1; j
< n
; j
++){
60 int answer
= a
[middle
];
63 answer
+= a
[middle
- 1];
71 int main(int argc
, char **argv
)
73 int a
[] = {1, 3, 4, -3231, 5, 99, 2};
74 int b
[] = {1, 3, 4, 5, 99, 2, 88, 88, 88, 55, 6};
75 int c
[] = { -1, -4, 0, 3};
78 printf("%d\n", median(a
, sizeof(a
) / sizeof(int)));
79 printf("%d\n", median(b
, sizeof(b
) / sizeof(int)));
80 printf("%d\n", median(c
, sizeof(c
) / sizeof(int)));
81 printf("%d\n", median(d
, sizeof(d
) / sizeof(int)));
83 printf("%d\n", sort_median(a
, sizeof(a
) / sizeof(int)));
84 printf("%d\n", sort_median(b
, sizeof(b
) / sizeof(int)));
85 printf("%d\n", sort_median(c
, sizeof(c
) / sizeof(int)));
86 printf("%x\n", sort_median(d
, sizeof(d
) / sizeof(int)));