-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgeneralFunctions.cpp
206 lines (178 loc) · 5.45 KB
/
generalFunctions.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "generalFunctions.h"
#include "psedoRandomNumbers.h"
#include "euclideanNode.h"
#include "dataTypes.h"
#include "clusterNode.h"
#include <sstream>
#include <limits>
using namespace std;
void bubleSort(double* t, int no)
{
double temp;
for(int i=0;i<no;i++) //taksinomoume ton pinaka
{
for(int j=0;j<no-i;j++)
{
if(j!=(no-1)&&t[j]>t[j+1])
{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
}
}
}
}
/*void quickSort(double* anArray, int start, int finish)
{
int middle;
if(start<finish)
{
middle = partisionRandom(anArray, start, finish);
quickSort(anArray, start, middle-1);
quickSort(anArray, middle+1, finish);
}
}
int partisionRandom(double* anArray, int start, int finish)
{
double val, temp;
int i, q;
q = start+ randomNumberInt(0, finish-start);
val = anArray[q];
anArray[q] = anArray[finish]; //antikatastoume to stoixeio q me to teleutaio
anArray[finish] = val; //kai vazoume to q sto telos
i = start-1;
for(int j = start; j <finish; j++)
{
if(anArray[j]<=val)
{
i++;
temp = anArray[i];
anArray[i] = anArray[j];
anArray[j] = temp;
}
}
temp = anArray[i+1];
anArray[i+1] = anArray[finish];
anArray[finish] = temp;
return i+1;
}
*/
template <class T>
void quickSort(NodeDistance<T>* anArray, int start, int finish)
{
int middle;
if(start<finish)
{
middle = partisionRandom(anArray, start, finish);
quickSort(anArray, start, middle-1);
quickSort(anArray, middle+1, finish);
}
}
template <class T>
int partisionRandom(NodeDistance<T>* anArray, int start, int finish)
{
NodeDistance<T> val, temp;
int i, q;
q = start+ randomNumberInt(0, finish-start);
val = anArray[q];
anArray[q] = anArray[finish]; //antikatastoume to stoixeio q me to teleutaio
anArray[finish] = val; //kai vazoume to q sto telos
i = start-1;
for(int j = start; j <finish; j++)
{
if(anArray[j].dist<=val.dist)
{
i++;
temp = anArray[i];
anArray[i] = anArray[j];
anArray[j] = temp;
}
}
temp = anArray[i+1];
anArray[i+1] = anArray[finish];
anArray[finish] = temp;
return i+1;
}
template <class T>
void sortPartOfArray(NodeDistance<T>* anArray, int size, int SortedItems, TYPE type) //SortedItems has to be small, otherwise call quickSort
{//similar to selection Sort
NodeDistance<T> maxDist, minDist, temp;
int pos;
for(int i = 0; i < SortedItems; i++)
{
maxDist.dist = -std::numeric_limits<double>::min();
minDist.dist = std::numeric_limits<double>::max();
for(int j = i; j <size; j++)
{
if(type == MAX)
{
if(anArray[j].dist > maxDist.dist)
{
maxDist = anArray[j];
pos = j;
}
}
else
{
if(anArray[j].dist < minDist.dist)
{
minDist = anArray[j];
pos = j;
}
}
}
//cout<<"max "<<maxDist.dist<<endl;
if(type == MAX)
{
temp = anArray[i];
anArray[i] = maxDist;
anArray[pos] = temp;
}
else
{
temp = anArray[i];
anArray[i] = minDist;
anArray[pos] = temp;
}
}
}
double maxDistance(double* distances, int size)
{
double maxDist = -std::numeric_limits<double>::min();
for(int i =0; i < size; i++)
{
if(distances[i] > maxDist)
{
maxDist = distances[i];
}
}
return maxDist;
}
int convertToBinary(char* bitString, int stringLength)
{
int number = 0;
int twoPower = 1;
for(int i = stringLength-1; i >= 0; i--)
{
number += bitString[i]*twoPower;//(bitString[i]-'0')*twoPower; //we have the askii value of char 1 and 0, to get the value 1 or 0 we get the minus the askii value of '0'
twoPower *=2;
}
return number;
}
std::string convertToString(int number)
{
std::stringstream s; //make int to string
s << number;
return s.str();
}
//template int partisionRandom(NodeDistance<ClusterNode<MatrixPoint*>*>* anArray, int start, int finish);
template void quickSort(NodeDistance<ClusterNode<MatrixPoint*>*>* anArray, int start, int finish);
template void quickSort(NodeDistance<ClusterNode<Hamming*>*>* anArray, int start, int finish);
template void quickSort(NodeDistance<ClusterNode<Vector*>*>* anArray, int start, int finish);
template void quickSort(NodeDistance<ClusterNode<EuclideanNode*>*>* anArray, int start, int finish);
template void quickSort(NodeDistance<int>* anArray, int start, int finish);
template void quickSort(NodeDistance<char>* anArray, int start, int finish);
template void sortPartOfArray(NodeDistance<int>* anArray, int size, int SortedItems, TYPE type);
template void sortPartOfArray(NodeDistance<ClusterNode<Hamming*>*>* anArray, int size, int SortedItems, TYPE type);
template void sortPartOfArray(NodeDistance<ClusterNode<EuclideanNode*>*>* anArray, int size, int SortedItems, TYPE type);
template void sortPartOfArray(NodeDistance<ClusterNode<Vector*>*>* anArray, int size, int SortedItems, TYPE type);