You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: Closest Pair/README.markdown
+3-3
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
Closest Pair is an algorithm that finds the closest pair of a given array of points By utilizing the Divide and Conquer methodology of solving problems so that it reaches the correct solution with O(nlogn) complexity.
4
4
5
-

5
+

6
6
7
7
As we see in the above image there are an array of points and we need to find the closest two, But how do we do that without having to compare each two points which results in a whopping O(n^2) complexity?
8
8
@@ -26,7 +26,7 @@ let line:Double = (p[mid].x + p[mid+1].x)/2
26
26
27
27
and just recursively calls itself until it reaches the base case we don't detect those points.
28
28
29
-

29
+

30
30
31
31
- To solve this we start by sorting the array on the Y-axis to get the points in their natural order and then we start getting the difference between the X position of the point and the line we drew to divide and if it is less than the min we got so far from the recursion we add it to the strip
32
32
@@ -46,7 +46,7 @@ while i<n
46
46
47
47
- After you insert the points that could possibly give you a better min distance we get to another observation in the image below.
48
48
49
-

49
+

50
50
51
51
- Searching the strip is a brute force loop (But doesn't that just destroy everything we did? You ask) but it has an advantage it could never iterate on more than 8 points (worst case).
0 commit comments