Closest-pair problem: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added whitespace.)
m (fix Swift version)
Line 4,337: Line 4,337:
guard xP.count > 3 else { return xP.closestPairBruteForce() }
guard xP.count > 3 else { return xP.closestPairBruteForce() }
let xl = Array(xP.prefix(xP.count / 2))
let half = xP.count / 2
let xr = Array(xP.suffix(xP.count / 2))
let xl = Array(xP[..<half])
let xm = xP.last!.x
let xr = Array(xP[half...])
let xm = xl.last!.x
let (yl, yr) = yP.reduce(into: ([Element](), [Element]()), {cur, el in
let (yl, yr) = yP.reduce(into: ([Element](), [Element]()), {cur, el in
if el.x > xm {
if el.x > xm {
Line 4,384: Line 4,385:
guard count != 2 else { return (minDistance, closestPoints) }
guard count != 2 else { return (minDistance, closestPoints) }
for i in 0..<count-1 {
for i in 0..<count {
for j in i+1..<count {
for j in i+1..<count {
let (iIndex, jIndex) = (index(startIndex, offsetBy: i), index(startIndex, offsetBy: j))
let (iIndex, jIndex) = (index(startIndex, offsetBy: i), index(startIndex, offsetBy: j))