Skip to content

Commit c40bcdb

Browse files
authored
closest_pair_of_points.py
1 parent 1e1ee00 commit c40bcdb

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

divide_and_conquer/closest_pair_of_points.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,24 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co
101101
# recursion
102102
mid = points_counts // 2
103103
closest_in_left = closest_pair_of_points_sqr(
104-
points_sorted_on_x, points_sorted_on_y[:mid], mid
105-
)
106-
closest_in_right = closest_pair_of_points_sqr(
107-
points_sorted_on_y, points_sorted_on_y[mid:], points_counts - mid
108-
)
109-
closest_pair_dis = min(closest_in_left, closest_in_right)
104+
points_sorted_on_x, points_sorted_on_y[:mid], mid
105+
)
106+
closest_in_right = closest_pair_of_points_sqr(
107+
points_sorted_on_x, points_sorted_on_y[mid:], points_counts - mid
108+
)
109+
closest_pair_dis = min(closest_in_left, closest_in_right)
110+
110111

111112
"""
112113
cross_strip contains the points, whose Xcoords are at a
113114
distance(< closest_pair_dis) from mid's Xcoord
114115
"""
116+
cross_strip = []
117+
mid_x = points_sorted_on_x[mid][0] # x-coordinate of the middle point
118+
for point in points_sorted_on_x:
119+
if abs(point[0] - mid_x) < closest_pair_dis:
120+
cross_strip.append(point)
115121

116-
cross_strip = []
117-
for point in points_sorted_on_x:
118-
if abs(point[0] - points_sorted_on_x[mid][0]) < closest_pair_dis:
119-
cross_strip.append(point)
120122

121123
closest_in_strip = dis_between_closest_in_strip(
122124
cross_strip, len(cross_strip), closest_pair_dis

0 commit comments

Comments
 (0)