今天我又写了一个归并排序双关键字的,但是又有问题,成绩能正确排序,但是有几个学号会变为0:
#include <stdio.h>
#include <stdlib.h>
void merge(int first,int last,int mid,int number[],int score[])
{
int score_temp[10]={0},number_temp[10]={0};
int i=first,j=mid+1,k;
for(k=0;k<=last-first;k++)
{
if(i==mid+1)
{
score_temp[k]=score[j++];
number_temp[k]=number[j];
continue;
}
if(j==last+1)
{
score_temp[k]=score[i++];
number_temp[k]=number[i];
continue;
}
if(score[i]<score[j])
score_temp[k]=score[i++],number_temp[k]=number[i];
else
score_temp[k]=score[j++],number_temp[k]=number[j];
}
j=0;
for(k=first;k<=last;k++,j++)
{
score[k]=score_temp[j];
number[k]=number_temp[j];
}
}
void merge_sort(int first,int last,int number[],int score[])
{
int mid=0;
if(first<last)
{
mid=(first+last)/2;
merge_sort(first,mid,number,score);
merge_sort(mid+1,last,number,score);
merge(first,last,mid,number,score);
}
}
int main()
{
int number[10]={0},score[10]={0},i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d %d",&number[i],&score[i]);
merge_sort(0,n-1,number,score);
printf("\n");
for(i=0;i<n;i++)
printf("%d %d\n",number[i],score[i]);
system("pause");
return 0;
}