@@ -47,29 +47,52 @@ void quickSort(point *vector, int leftIndex, int rightIndex){
47
47
return ;
48
48
}
49
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 ;
50
+ // Receives a vector and the position of two sorted subvectors inside it - merges both of them into a fully sorted one
51
+ void mergeSubvectors (point * vector , int firstLeftIndex , int firstRightIndex , int secondLeftIndex , int secondRightIndex ){
52
+ int vectorSize = (firstRightIndex - firstLeftIndex + 1 ) + (secondRightIndex - secondLeftIndex + 1 );
53
+
54
+ point * tmp = (point * )malloc (vectorSize * sizeof (point )); // Temporary vector in which the interpolation will occur
55
+
56
+ int i , j , k = 0 ; // Auxiliar variables that'll index the for-loops
57
+
58
+ // Loops through both subvectors, merging them
59
+ for (i = firstLeftIndex , j = secondLeftIndex ; k < vectorSize ; k ++ ){
60
+
61
+ // If there are still elements in both subvector
62
+ if (i <= firstRightIndex && j <= secondRightIndex ){
63
+ // Compares them and copies the lowest one to tmp
64
+ if (vector [i ].y < vector [j ].y ){ tmp [k ] = vector [i ++ ]; }
65
+ else { tmp [k ] = vector [j ++ ]; }
66
+ }
54
67
55
- while ( 1 ){
56
- // Selects elements from the left that are >= to the pivot
57
- while ( vector [ left ]. y < pivot ){ left ++ ; }
68
+ // If not, copies the remaining element to tmp
69
+ else if ( i <= firstRightIndex ){ tmp [ k ] = vector [ i ++ ]; }
70
+ else if ( j <= secondRightIndex ){ tmp [ k ] = vector [ j ++ ] ; }
58
71
59
- // Selects elements from the right that are <= to the pivot
60
- while (vector [right ].y > pivot ){ right -- ; }
72
+ }
61
73
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
74
+ // Copies the sorted vector in tmp to the original one, replacing the subvectors
75
+ for (i = firstLeftIndex , j = secondLeftIndex , k = 0 ; k < vectorSize ; k ++ ){
76
+ if (i <= firstRightIndex ){ vector [i ++ ] = tmp [k ]; }
77
+ else { vector [j ++ ] = tmp [k ]; }
64
78
65
- // If the swap is not worth it, the vector was successfully partitionted
66
- else { break ; }
79
+ }
67
80
68
- }
81
+ free ( tmp ); tmp = NULL ;
69
82
70
- // Sorts the partitions
71
- quickSort (vector , leftIndex , right );
72
- quickSort (vector , right + 1 , rightIndex );
83
+ return ;
84
+ }
85
+
86
+ // MERGE SORT
87
+ void mergeSort (point * vector , int leftIndex , int rightIndex ){
88
+ int vectorSize = rightIndex - leftIndex + 1 ;
89
+
90
+ if (vectorSize > 1 ){
91
+ int middleIndex = (rightIndex + leftIndex ) / 2 ;
92
+
93
+ mergeSort (vector , leftIndex , middleIndex );
94
+ mergeSort (vector , middleIndex + 1 , rightIndex );
95
+ mergeSubvectors (vector , leftIndex , middleIndex , middleIndex + 1 , rightIndex );
73
96
}
74
97
75
98
return ;
@@ -98,7 +121,8 @@ float auxGetLowestDist(point *points, int leftIndex, int rightIndex){
98
121
}
99
122
}
100
123
101
- quickSortY (strip , 0 , stripSize - 1 );
124
+ // quickSortY(strip, 0, stripSize-1);
125
+ mergeSort (strip , 0 , stripSize - 1 );
102
126
103
127
float div = (strip [0 ].x > points [leftIndex ].x && strip [stripSize - 1 ].x < points [rightIndex ].x )
104
128
? auxGetLowestDist (strip , 0 , stripSize - 1 )
0 commit comments