Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jun 14, 2021. It is now read-only.

Commit 2da75b1

Browse files
main-2.c --> 10/10
1 parent 422b950 commit 2da75b1

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

‎Atv-13/main-2‎

19.1 KB
Binary file not shown.

‎Atv-13/main-2.c‎

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,52 @@ void quickSort(point *vector, int leftIndex, int rightIndex){
4747
return;
4848
}
4949

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+
}
5467

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+
elseif(i <= firstRightIndex){ tmp[k] =vector[i++]; }
70+
elseif(j <= secondRightIndex){ tmp[k] =vector[j++]; }
5871

59-
// Selects elements from the right that are <= to the pivot
60-
while(vector[right].y > pivot){ right--; }
72+
}
6173

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]; }
6478

65-
// If the swap is not worth it, the vector was successfully partitionted
66-
else{ break; }
79+
}
6780

68-
}
81+
free(tmp); tmp=NULL;
6982

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);
7396
}
7497

7598
return;
@@ -98,7 +121,8 @@ float auxGetLowestDist(point *points, int leftIndex, int rightIndex){
98121
}
99122
}
100123

101-
quickSortY(strip, 0, stripSize-1);
124+
// quickSortY(strip, 0, stripSize-1);
125+
mergeSort(strip, 0, stripSize-1);
102126

103127
float div = (strip[0].x > points[leftIndex].x && strip[stripSize-1].x < points[rightIndex].x)
104128
? auxGetLowestDist(strip, 0, stripSize-1)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /