| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 581 人关注过本帖
标题:来求教!用快速排序排一个结构数组,帮忙看一下我的程序吧!
只看楼主 加入收藏
ivoryhouse
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-10-11
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:6 
来求教!用快速排序排一个结构数组,帮忙看一下我的程序吧!
大概就是输入n(<100000)个人的数据(名字、国家、财富),排出财富前m(<100)个人,并将财富按从大到小将每个人输出。我的程序编译貌似没问题,就运行的时候,输入第二个人的数据时就程序出问题。T T高人们帮我看一下啊...要怎么改?有什么要改进的地方也说一下啊!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define Error( Str ) FatalError( Str )
#define FatalError( Str ) fprintf( stderr, "%s\n", Str ), exit( 1 )

#define MaxN 100000
#define MaxM 100
#define Cutoff ( 3 )

typedef struct people_list *PtrToList;
typedef PtrToList Person;
struct people_list{   
   char name[20];   
   char country[20];   
   int money;
};
typedef int ElementType;
void InsertionSort( Person A[ ], int N );
void Swap(Person A, Person B);
ElementType Median3( Person A[ ], int Left, int Right );
void Qsort( Person A[], int Left, int Right );


int main(void){   
   int n,m,i,j;   
   printf("enter N and M:");   
   scanf("%d%d",&n,&m);   
   Person people[MaxN];   
   printf("Enter name, nationality and wealth:\n");   
   for(i=0;i<n;i++){   
      scanf("%s%s%d",people[i]->name,people[i]->country,&people[i]->money);   
   }   
   Qsort(people,0,n-1);   
   for(j=n-1;j>=n-m;j--){   
      printf("%s %s %d\n",people[j]->name,people[j]->country,people[j]->money);   
   }   
   return 0;
}

void Swap( Person A, Person B )
{   
   Person Tmp;   
   Tmp=A;   
   A=B;   
   B=Tmp;
}

void InsertionSort( Person A[ ], int N )
{
   int j, P;   
   Person Tmp;
   for( P = 1; P < N; P++ )   {
      Tmp=A[P];   
   for( j = P; j > 0 && A[ j - 1 ]->money > Tmp->money; j-- )
      A[ j ]= A[ j - 1 ];     
   A[ j ]= Tmp;   
   }
}

ElementType Median3( Person A[ ], int Left, int Right )
{   
   int Center = ( Left + Right ) / 2;
   if( A[ Left ]->money > A[ Center ]->money )
      Swap( A[ Left ], A[ Center ] );
   if( A[ Left ]->money > A[ Right ]->money )
      Swap( A[ Left ], A[ Right ]);
   if( A[ Center ]->money > A[ Right ]->money )
      Swap( A[ Center ], A[ Right ]);
   Swap( A[ Center ], A[ Right - 1 ] ); /* Hide pivot */
   return A[ Right - 1 ]->money;
}

#define Cutoff ( 3 )
void Qsort( Person A[],int Left, int Right )
{   int i, j;
   ElementType Pivot;
   if( Left + Cutoff <= Right )   {
      Pivot = Median3( A, Left, Right );
   i = Left; j = Right - 1;
   for( ; ; )   {
      while( A[ ++i ]->money < Pivot ){ }
      while( A[ --j ]->money > Pivot ){ }
      if( i < j )
         Swap( A[ i ], A[ j ]);
      else
         break;
   }
   Swap( A[ i ], A[ Right - 1 ]); /* Restore pivot */
   Qsort( A, i + 1, Right );
   Qsort( A, Left, i - 1 );
   }
   else /* Do an insertion sort on the subarray */
      Insertion Sort( A+Left, Right - Left + 1 );
}
搜索更多相关主题的帖子: 结构 
2010-10-11 14:47
逐渐学习
Rank: 6Rank: 6
等 级:侠之大者
帖 子:113
专家分:454
注 册:2010-9-26
收藏
得分:20 
int main(void){   
   int n,m,i,j;   
   printf("enter N and M:");   
   scanf("%d%d",&n,&m);   
   Person people[MaxN];   
   printf("Enter name, nationality and wealth:\n");   
   for(i=0;i<n;i++){   
      people[i]=(people_list *)malloc(sizeof(people_list));//增加开辟内存空间
      scanf("%s%s%d",people[i]->name,people[i]->country,&people[i]->money);   
   }   
   Qsort(people,0,n-1);   
   for(j=n-1;j>=n-m;j--){   
      printf("%s %s %d\n",people[j]->name,people[j]->country,people[j]->money);   
   }
   for(i=0;i<n;i++){   
      free(people[i]);

   }
   return 0;
}
把蓝色部分加上去,在看看。

帮人《---》帮己
2010-10-11 15:44
ivoryhouse
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-10-11
收藏
得分:0 
回复 2楼 逐渐学习
谢谢啊!..试了!...输入数据是没问题了...可是输出的数据排序还是老样子,没进行快速排序...怎么回事??!
2010-10-11 16:15
逐渐学习
Rank: 6Rank: 6
等 级:侠之大者
帖 子:113
专家分:454
注 册:2010-9-26
收藏
得分:0 
int main(void){   
   int n,m,i,j;   
   printf("enter N and M:");   
   scanf("%d%d",&n,&m);   
   Person people[MaxN];   
   printf("Enter name, nationality and wealth:\n");   
   for(i=0;i<n;i++){   
      people[i]=(people_list *)malloc(sizeof(people_list));//增加开辟内存空间
      scanf("%s%s%d",people[i]->name,people[i]->country,&people[i]->money);   
   }   
   Qsort(people,0,n-1);   
   //for(j=n-1;j>=n-m;j--)
   for(j=m-1;j>=0;j--){   
      printf("%s %s %d\n",people[j]->name,people[j]->country,people[j]->money);   
   }
   for(i=0;i<n;i++){   
      free(people[i]);
   }
   return 0;
}
我能力有限,还没全看懂你的程序,用蓝色部分改改吧,我试试好像可以了。

[ 本帖最后由 逐渐学习 于 2010-10-11 16:39 编辑 ]

帮人《---》帮己
2010-10-11 16:30
ivoryhouse
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-10-11
收藏
得分:0 
回复 4楼 逐渐学习
谢谢呐!! 我又看了半天.....原来那个void Swap( Person A, Person B )
要改成void Swap( Person &A, Person &B )
基础不扎实啊......
2010-10-11 17:37
tzp876301129
Rank: 2
等 级:论坛游民
帖 子:29
专家分:31
注 册:2010-5-16
收藏
得分:0 
不会
哎!
能力有限
2010-10-24 15:57
快速回复:来求教!用快速排序排一个结构数组,帮忙看一下我的程序吧!
数据加载中...
 
   



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

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