gitignore ignorable gtksparrow executable
[sparrow.git] / median.h
blob8de7d47e8c39a91ef3e0c06f37d65b5ed20a864d
1 /*
2 * Based on Nicolas Devillard's work at
4 * http://ndevilla.free.fr/median/median/index.html
5 * http://ndevilla.free.fr/median/median/src/wirth.c
6 * ndevilla AT free DOT fr
8 * His header:
10 * Algorithm from N. Wirth's book, implementation by N. Devillard.
11 * This code in public domain.
15 static inline coord_t
16 coord_median(coord_t *values, unsigned int n)
18 int middle = n / 2;
19 int i, j; /* working bottom, top */
20 int bottom = 0; /* window bottom */
21 int top = n - 1; /*window top */
23 if (! (n & 1)){ /*return lower of 2 centre values */
24 middle--;
27 while (bottom < top) {
28 i = bottom;
29 j = top;
30 do {
31 while (values[i] < values[middle]){
32 i++;
34 while (values[middle] < values[j]){
35 j--;
37 if (i <= j){
38 coord_t tmp = values[i];
39 values[i] = values[j];
40 values[j] = tmp;
41 i++;
42 j--;
44 } while (i <= j);
46 if (j < middle){
47 bottom = i;
49 if (middle < i){
50 top = j;
53 return values[middle];