-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.cpp
More file actions
59 lines (48 loc) · 1.02 KB
/
filter.cpp
File metadata and controls
59 lines (48 loc) · 1.02 KB
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
#include "filter.h"
#include <algorithm>
using namespace std;
int partition(int *array, int left, int right)
{
if (array == NULL)
return -1;
int pos = right;
right--;
while (left <= right)
{
while (left < pos && array[left] <= array[pos])
left++;
while (right >= 0 && array[right] > array[pos])
right--;
if (left >= right)
break;
swap(array[left], array[right]);
}
swap(array[left], array[pos]);
return left;
}
double findMedian (int *array, int size)
{
if (array == NULL || size <= 0)
return -1;
int left = 0;
int right = size - 1;
int midPos = right >> 1;
int index = -1;
while (index != midPos)
{
index = partition(array, left, right);
if (index < midPos)
{
left = index + 1;
}
else if (index > midPos)
{
right = index - 1;
}
else
{
break;
}
}
return array[index];
}