-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.cpp
75 lines (69 loc) · 1.76 KB
/
tree.cpp
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
71
72
73
74
75
int countIsIn(const double a1[], int n1, const double a2[], int n2)
{
if (n1 <= 0) { //Base and edge cases
return 1;
}
if (n2 <= 0) {
return 0;
}
if (a1[0] == a2[0]) { //Increment both if they are the same and add to case where elem is repeated twice
return countIsIn(a1 + 1, n1 - 1, a2 + 1, n2 - 1) + countIsIn(a1, n1, a2+1, n2-1);
}
else {
return countIsIn(a1, n1, a2+1, n2-1);
}
}
// Exchange two doubles
void exchange(double& x, double& y)
{
double t = x;
x = y;
y = t;
}
void divide(double a[], int n, double divider,
int& firstNotGreater, int& firstLess)
{
if (n < 0)
n = 0;
/* It will always be the case that just before evaluating the loop
condition:
firstNotGreater <= firstUnknown and firstUnknown <= firstLess
Every element earlier than position firstNotGreater is > divider
Every element from position firstNotGreater to firstUnknown-1 is
== divider
Every element from firstUnknown to firstLess-1 is not known yet
Every element at position firstLess or later is < divider */
firstNotGreater = 0;
firstLess = n;
int firstUnknown = 0;
while (firstUnknown < firstLess)
{
if (a[firstUnknown] < divider)
{
firstLess--;
exchange(a[firstUnknown], a[firstLess]);
}
else
{
if (a[firstUnknown] > divider)
{
exchange(a[firstNotGreater], a[firstUnknown]);
firstNotGreater++;
}
firstUnknown++;
}
}
}
//Rearrange the elements of the array so that
//a[0] >= a[1] >= a[2] >= ... >= a[n-2] >= a[n-1]
//If n <= 1, do nothing.
void order(double a[], int n)
{
if (n <= 1) {
return;
}
int firstNotGreater, firstLess;
divide(a, n, a[0], firstNotGreater, firstLess);
order(a, firstNotGreater);
order(a + firstLess, n - firstLess);
}