option to use video test source for testing with camera
[sparrow.git] / test-median.c
blobef4835915962dc527d9a3b8c1f91441db2ecef0f
1 //#include "sparrow.h"
2 #include <string.h>
3 #include <stdio.h>
4 #include <stdint.h>
6 int median(int *a, unsigned int n)
8 int middle = n / 2;
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 */
14 middle--;
17 while (bottom < top) {
18 i = bottom;
19 j = top;
20 do {
21 while (a[i] < a[middle]){
22 i++;
24 while (a[middle] < a[j]){
25 j--;
27 if (i <= j){
28 int tmp = a[i];
29 a[i] = a[j];
30 a[j] = tmp;
31 i++;
32 j--;
34 } while (i <= j);
36 if (j < middle){
37 bottom = i;
39 if (middle < i){
40 top = j;
43 return a[middle];
46 int sort_median(int *a, int n)
48 int i, j;
49 /*stupid sort, but n is very small*/
50 for (i = 0; i < n; i++){
51 for (j = i + 1; j < n; j++){
52 if (a[i] > a[j]){
53 int tmp = a[j];
54 a[j] = a[i];
55 a[i] = tmp;
59 int middle = n >> 1;
60 int answer = a[middle];
62 if ((n & 1) == 0){
63 answer += a[middle - 1];
64 answer /= 2;
66 return answer;
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};
76 int d[] = {3, 5};
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)));