shell排序

eeetttzhangji 2016-10-05 05:32:37
用shell排序的方法对奥运金牌榜进行排序(多关键字),我严格按照shell排序的模板写的,可就是不对,麻烦给看一下哪里错了。

[code=c#include<stdio.h>
#include<string.h>


struct medal
{
char name[10];
int gold;
int sliv;
int bron;
};

int comp(const void *a,const void *b);
void swap(char *a,char *b,int width);
void shellsort(void *x,int n,int size,int (*fcmp)(const void *a,const void *b));
void Print(struct medal a[],int n);
int main()
{
struct medal country[10]={
{"Japan",1,3,1},
{"America",8,3,1},
{"Germany",2,5,6},
{"England",3,2,5},
{"France",4,8,6},
{"Canada",2,6,0},
{"Korea",5,3,6},
{"Australia",3,2,6},
{"India",1,3,1},
{"China",8,3,1}
};
shellsort(country,10,sizeof(country[0]),comp);
Print(country,10);
return 0;
}

int comp(const void *a,const void *b)
{
struct medal *pa;
struct medal *pb;
pa=(struct medal *)a;
pb=(struct medal *)b;

if(pa->gold != pb->gold)
return pa->gold - pb->gold;

if(pa->sliv != pb->sliv)
return pa->sliv - pb->sliv;

if(pa->bron != pb->bron)
return pa->bron - pb->bron;

return strcmp(pb->name,pa->name);
}

void swap(char *a,char *b,int width)
{
if(a!=b)
{
while(width--){
*b=*a;
a++;
b++;
}
}
}

void shellsort(void *x,int n,int size,int (*fcmp)(const void *,const void *))
{
struct medal temp;
struct medal *tp ,*gp,*gq;
tp=(struct medal *)x;
int gap,gapwidth,i;
char *p,*q;
p=(char *)x;
for(gap=n/2;gap>=1;gap/=2)
{
for(i=gap;i<n;i++)
{
temp=tp[i];
gapwidth=gap*size;
for(q=p+(i-gap)*size; q>=p && comp(p+i*size,q)>0 ; q-=gapwidth)
swap(q,q+gapwidth,size);
gp=(struct medal *)(q+gapwidth);
strcmp(gp->name,temp.name);
gp->gold=temp.gold;
gp->sliv=temp.sliv;
gp->bron=temp.bron;

}
}
}

void Print(struct medal a[],int n)
{
int i;
printf("\n 国家 金牌 银牌 铜牌 \n");

for(i=0;i<n;i++)
{
printf("%10s%8d%8d%8d\n",a[i].name,a[i].gold,a[i].sliv,a[i].bron);
}
}][/code]
...全文
647 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ID870177103 2016-10-07
  • 打赏
  • 举报
回复
template <class T>
void InsertSortImpl (std::vector<T> &a ,int step) {
	for (int i = 0 ; i < step ; i++)
		for (int j = i ; j < int (a.size ()) ; j += step) {
			T bak = a[j] ;
			int k ;
			for (k = j ; k - step >= i && bak < a[k - step] ; k -= step)
				a[k] = a[k - step] ;
			a[k] = bak ;
		}
}

template <class T>
void InsertSort (std::vector<T> &a) {
	InsertSortImpl (a ,1) ;
}

template <class T>
void ShellSort (std::vector<T> &a) {
	for (int i = 3 ; i >= 1 ; i--)
		InsertSortImpl (a ,i) ;
}

struct medal {
	char name[10];
	int gold;
	int sliv;
	int bron;

	inline bool operator< (const medal &r) const {
		if (gold != r.gold)
			return gold < r.gold ;
		if (sliv != r.sliv)
			return sliv < r.sliv;
		if (bron != r.bron)
			return bron < r.bron;
		return strcmp (name ,r.name) < 0 ;
	}

	friend inline std::ostream &operator<< (std::ostream &os ,const medal &l) {
		return os << "{" << l.name << " ," << l.gold << " ," << l.sliv << " ," << l.bron << "}" ;
	}
} ;

int main () {
	std::vector<medal> a = {
		{"Japan" ,1 ,3 ,1} ,
		{"America" ,8 ,3 ,1} ,
		{"Germany" ,2 ,5 ,6} ,
		{"England" ,3 ,2 ,5} ,
		{"France" ,4 ,8 ,6} ,
		{"Canada" ,2 ,6 ,0} ,
		{"Korea" ,5 ,3 ,6} ,
		{"Australia" ,3 ,2 ,6} ,
		{"India" ,1 ,3 ,1} ,
		{"China" ,8 ,3 ,1}
	} ;
	std::vector<medal> b = a ;
	InsertSort (b) ;
	std::vector<medal> c = a ;
	ShellSort (c) ;
	for (int i = 0 ; i < int (a.size ()) ; i++)
		std::cout << b[i] << "\t\t" << c[i] << std::endl ;
	std::cout << std::endl ;
	return 0 ;
}
eeetttzhangji 2016-10-07
  • 打赏
  • 举报
回复
你帮我找一下我的代码错那了吧,我自己总是找不出来了,我只会C语言,你的代码我看不懂,我的代码和书上给出的代码就只有
for(q=p+(i-gap)*size; q>=p && comp(p+i*size,q)>0 ; q-=gapwidth)
swap(q,q+gapwidth,size);
gp=(struct medal *)(q+gapwidth);
strcmp(gp->name,temp.name);
gp->gold=temp.gold;
gp->sliv=temp.sliv;
gp->bron=temp.bron;
问题应该就出在上面这段代码中。
eeetttzhangji 2016-10-07
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<string.h>


struct medal
{
char name[10];
int gold;
int sliv;
int bron;
};

int comp(const void *a,const void *b);
void swap(char *a,char *b,int width);
void shellsort(void *x,int n,int size,int (*fcmp)(const void *a,const void *b));
void Print(struct medal a[],int n);
int main()
{
struct medal country[10]={
{"Japan",1,3,1},
{"America",8,3,1},
{"Germany",2,5,6},
{"England",3,2,5},
{"France",4,8,6},
{"Canada",2,6,0},
{"Korea",5,3,6},
{"Australia",3,2,6},
{"India",1,3,1},
{"China",8,3,1}
};
shellsort(country,10,sizeof(country[0]),comp);
Print(country,10);
return 0;
}

int comp(const void *a,const void *b)
{
struct medal *pa;
struct medal *pb;
pa=(struct medal *)a;
pb=(struct medal *)b;

if(pa->gold != pb->gold)
return pa->gold - pb->gold;

if(pa->sliv != pb->sliv)
return pa->sliv - pb->sliv;

if(pa->bron != pb->bron)
return pa->bron - pb->bron;

return strcmp(pb->name,pa->name);
}

void swap(char *a,char *b,int width)
{
if(a!=b)
{
while(width--){
*b=*a;
a++;
b++;
}
}
}

void shellsort(void *x,int n,int size,int (*fcmp)(const void *,const void *))
{
struct medal temp;
struct medal *tp ,*gp,*gq;
tp=(struct medal *)x;
int gap,gapwidth,i;
char *p,*q;
p=(char *)x;
for(gap=n/2;gap>=1;gap/=2)
{
for(i=gap;i<n;i++)
{
temp=tp[i];
gapwidth=gap*size;
for(q=p+(i-gap)*size; q>=p && comp(p+i*size,q)>0 ; q-=gapwidth)
swap(q,q+gapwidth,size);
gp=(struct medal *)(q+gapwidth);
strcmp(gp->name,temp.name);
gp->gold=temp.gold;
gp->sliv=temp.sliv;
gp->bron=temp.bron;

}
}
}

void Print(struct medal a[],int n)
{
int i;
printf("\n     国家     金牌     银牌     铜牌     \n");

for(i=0;i<n;i++)
{
printf("%10s%8d%8d%8d\n",a[i].name,a[i].gold,a[i].sliv,a[i].bron);
}
}

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧