@@ -19,17 +19,17 @@ void swapElements(point *vector, int i, int j){
19
19
return ;
20
20
}
21
21
22
- void quickSort (point * vector , int leftIndex , int rightIndex , int x ){
22
+ void quickSort (point * vector , int leftIndex , int rightIndex ){
23
23
if (leftIndex < rightIndex ){
24
- float pivot = x ? vector [(int )((rightIndex + leftIndex )/2 )].x : vector [( int )(( rightIndex + leftIndex )/ 2 )]. y ;
24
+ float pivot = vector [(int )((rightIndex + leftIndex )/2 )].x ;
25
25
int left = leftIndex , right = rightIndex ;
26
26
27
27
while (1 ){
28
28
// Selects elements from the left that are >= to the pivot
29
- while (x ? ( vector [left ].x < pivot ) : ( vector [ left ]. y < pivot ) ){ left ++ ; }
29
+ while (vector [left ].x < pivot ){ left ++ ; }
30
30
31
31
// Selects elements from the right that are <= to the pivot
32
- while (x ? ( vector [right ].x > pivot ) : ( vector [ left ]. y > pivot ) ){ right -- ; }
32
+ while (vector [right ].x > pivot ){ right -- ; }
33
33
34
34
// If the swapping the above selected elements is worth it, do it
35
35
if (left < right ){ swapElements (vector , left ++ , right -- ); } // And pass to the next element
@@ -40,8 +40,36 @@ void quickSort(point *vector, int leftIndex, int rightIndex, int x){
40
40
}
41
41
42
42
// Sorts the partitions
43
- quickSort (vector , leftIndex , right , x );
44
- quickSort (vector , right + 1 , rightIndex , x );
43
+ quickSort (vector , leftIndex , right );
44
+ quickSort (vector , right + 1 , rightIndex );
45
+ }
46
+
47
+ return ;
48
+ }
49
+
50
+ void quickSortY (point * vector , int leftIndex , int rightIndex ){
51
+ if (leftIndex < rightIndex ){
52
+ float pivot = vector [(int )((rightIndex + leftIndex )/2 )].y ;
53
+ int left = leftIndex , right = rightIndex ;
54
+
55
+ while (1 ){
56
+ // Selects elements from the left that are >= to the pivot
57
+ while (vector [left ].y < pivot ){ left ++ ; }
58
+
59
+ // Selects elements from the right that are <= to the pivot
60
+ while (vector [right ].y > pivot ){ right -- ; }
61
+
62
+ // If the swapping the above selected elements is worth it, do it
63
+ if (left < right ){ swapElements (vector , left ++ , right -- ); } // And pass to the next element
64
+
65
+ // If the swap is not worth it, the vector was successfully partitionted
66
+ else { break ; }
67
+
68
+ }
69
+
70
+ // Sorts the partitions
71
+ quickSort (vector , leftIndex , right );
72
+ quickSort (vector , right + 1 , rightIndex );
45
73
}
46
74
47
75
return ;
@@ -70,19 +98,19 @@ float auxGetLowestDist(point *points, int leftIndex, int rightIndex){
70
98
}
71
99
}
72
100
73
- quickSort (strip , 0 , stripSize - 1 , 0 );
101
+ quickSortY (strip , 0 , stripSize - 1 );
74
102
75
- for ( int i = 0 ; i < stripSize ; i ++ ){
76
- for ( int j = i + 1 ; j < stripSize && (strip [ j ]. y - strip [ i ]. y < distance ); j ++ ){
77
- distance = min ( distance , d ( strip [ i ], strip [ j ])) ;
78
- }
79
- }
103
+ float div = ( strip [ 0 ]. x > points [ leftIndex ]. x && strip [ stripSize - 1 ]. x < points [ rightIndex ]. x )
104
+ ? auxGetLowestDist (strip , 0 , stripSize - 1 )
105
+ : -1 ;
106
+
107
+ free ( strip );
80
108
81
- return distance ;
109
+ return min ( distance , div ) ;
82
110
}
83
111
84
112
float getLowestDist (point * points , int n ){
85
- quickSort (points , 0 , n - 1 , 1 );
113
+ quickSort (points , 0 , n - 1 );
86
114
return auxGetLowestDist (points , 0 , n - 1 );
87
115
}
88
116
0 commit comments