Anonymous user
Closest-pair problem: Difference between revisions
→{{header|D}}
Line 214:
<lang d>import std.stdio,std.typecons,std.math,std.algorithm,std.array,std.random;
auto
auto minD = typeof(points[0].re).infinity;
foreach (i; 0 .. points.length-1)
foreach (j; i+1 .. points.length) {
Line 222:
if (dist < minD) {
minD = dist;
minI = points[i];
minJ = points[j];
}
}
return tuple(minD,
}
Line 290:
A faster brute-force version:
<lang d>import std.stdio, std.typecons, std.math, std.random;
auto bruteForceClosestPair2(cdouble[] points) {
auto minD = typeof(points[0].re).infinity;
if (points.length < 2)
return tuple(minD, typeof(points[0]).nan, typeof(points[0]).nan);
size_t minI, minJ;
foreach (i; 0 .. points.length-1) {
auto points_i_re = points[i].re;
auto points_i_im = points[i].im;
foreach (j; i+1 .. points.length) {
auto dre =
auto
if (dist < minD) {
minD = dist;
minI = i;
minJ = j;
}
}
}
}
return tuple(sqrt(minD), points[minI], points[minJ]);
}
void main() {
auto rnd = Random(1);
auto points = new cdouble[10_000];
foreach (ref p; points)
p = uniform(0.0, 1000.0, rnd) + uniform(0.0, 1000.0, rnd) * 1i;
writeln("bruteForceClosestPair2: ", bruteForceClosestPair2(points));
}
Line 315 ⟶ 331:
writeln("bruteForceClosestPair2: ", bruteForceClosestPair2(points));
}</lang>
Time gave 0:
=={{header|F Sharp|F#}}==
|