| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 8130 人关注过本帖
标题:归并排序
取消只看楼主 加入收藏
sunyh1999
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:14
帖 子:1178
专家分:3032
注 册:2009-5-17
收藏
得分:0 
什么意思?

欢迎来到我的博客:http://blog..cn/noisunyuhong
2011-02-01 13:38
sunyh1999
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:14
帖 子:1178
专家分:3032
注 册:2009-5-17
收藏
得分:0 
写好了且测试数据过了,问题在于j++上,要先算再+
#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];
    j++;
    continue;
    }
    if(j==last+1)
    {
    score_temp[k]=score[i];
    number_temp[k]=number[i];
    i++;
    continue;
    }
    if(score[i]<score[j])  score_temp[k]=score[i],number_temp[k]=number[i],i++;
    else if(score[i]==score[j]&&number[i]>number[j])  score_temp[k]=score[i],number_temp[k]=number[i],i++;
    else
    score_temp[k]=score[j],number_temp[k]=number[j],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;
}

欢迎来到我的博客:http://blog..cn/noisunyuhong
2011-02-01 16:08
快速回复:归并排序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014868 second(s), 8 queries.
Copyright©2004-2025, BC-CN.NET, All Rights Reserved