1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
void EnterMessage();
void EnterPoints();
void SortedPoints();
void TopDownMergeSort(int A[],int B[],int n,int sorter_point[]);
void TopDownSplitMerge(int B[],int iBegin,int iEnd,int A[],int sorter_point[]);
void TopDownMerge(int A[],int iBegin,int iMiddle,int iEnd,int B[],int sorter_point[]);
void CopyArray(int A[],int iBegin,int iEnd,int B[]);
int main()
{
EnterMessage();
int number_points;
scanf(" %d",&number_points);
int points[number_points][number_points];
int distance[number_points];
int help_array[number_points];
int sorter_points[number_points];
for (int i=0;i<number_points;i++){
EnterPoints();
for (int j=0;j<2;j++){
scanf(" %d", &points[i][j]);
distance[i]+=(points[i][j]*points[i][j]);}
}
TopDownMergeSort(distance,help_array,number_points,sorter_points);
printf("%d",sorter_points[0]);
return 0;
}
void EnterMessage(){
printf("Enter number of points: ");}
void EnterPoints(){
printf("Enter point: ");}
void SortedPoints(){
printf("Sorted points: ");}
void TopDownMergeSort(int A[],int B[],int n,int sorter_point[])
{
CopyArray(A, 0, n, B);
TopDownSplitMerge(B, 0, n, A,sorter_point);
}
void TopDownSplitMerge(int B[],int iBegin,int iEnd,int A[],int sorter_point[])
{
if(iEnd - iBegin < 2)
return;
int iMiddle = (iEnd + iBegin) / 2;
TopDownSplitMerge(A, iBegin, iMiddle, B,sorter_point);
TopDownSplitMerge(A, iMiddle,iEnd, B,sorter_point);
TopDownMerge(B, iBegin, iMiddle, iEnd, A,sorter_point);
}
void TopDownMerge(int A[],int iBegin,int iMiddle,int iEnd,int B[],int sorter_point[])
{
int counter=0;
int i = iBegin, j = iMiddle;
for (int k = iBegin; k < iEnd; k++) {
if (i < iMiddle && (j >= iEnd || A[i] <= A[j])) {
B[k] = A[i];
sorter_point[counter]=i;
i = i + 1;
counter++;
} else {
B[k] = A[j];
sorter_point[counter]=j;
j = j + 1;
counter++;
}
}
}
void CopyArray(int A[],int iBegin,int iEnd,int B[])
{
for(int k = iBegin; k < iEnd; k++)
B[k] = A[k];
} |
Partager