结构体数组怎么排序?

order_c 2012-11-14 06:33:50
如果排序的过程中要交换关键字的值,那不是其他成员的值也要交换?
...全文
4021 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
order_c 2012-11-14
  • 打赏
  • 举报
回复
引用 11 楼 zhuankeshumo 的回复:
时间和空间不可兼得
谢谢
newtee 2012-11-14
  • 打赏
  • 举报
回复
时间和空间不可兼得
order_c 2012-11-14
  • 打赏
  • 举报
回复
引用 4 楼 JiMoKuangXiangQu 的回复:
用结构体的指针数组来排序就好了,交换的时候只交换指针就可以了. 如: #define N 20 struct my_struct { int i; char chars[100]; float f; } structs[N]; struct my_struct *pointers[N]; int i; for (i = ……
这个方法不错
冰与火 2012-11-14
  • 打赏
  • 举报
回复
光交换“关键字”是错误的,如果这样你不仅没交换结构体,还改变了原来结构体的值(“关键字”交换了)
order_c 2012-11-14
  • 打赏
  • 举报
回复
引用 5 楼 zhuankeshumo 的回复:
如果排序的过程中要交换关键字的值,那不是其他成员的值也要交换? 可以直接交换结构体
如果结构体成员很少,可以直接交换,但如果成员很多,交换结构体效率有点低吧!
newtee 2012-11-14
  • 打赏
  • 举报
回复 1
仅供参考
#include<stdio.h>
#include<stdlib.h>
#define n 10
struct student{
	char name[10];
	int score;
};
void shellsort(struct student a[n],int m);
void quicksort(struct student a[n],int low,int high);
void bubblesort(struct student a[n],int m);
void print(struct student a[n]);
void main()
{
	struct student a[n]={{"",0},{"wwq",75},{"ww",75},{"rr11",67},{"uu",67},{"q1",80},{"w2",75},{"r2r",69},{"u2u",67},{"www",100}};
	char s[5];
	for(;;)
	{
      printf("1------------希尔排序\n");
      printf("2------------快速排序\n");
	  printf("3------------冒泡排序\n");
	  printf("4------------退出\n");
	  gets(s);
	  switch(*s)
	  {
	      case '1':shellsort(a,9);
			       print(a);
				   break;
	      case '2':quicksort(a,1,9);
			       print(a);
				   break;  
		  case '3':bubblesort(a,9);
			       print(a);
				   break;				  
		  case '4':exit(0);
	      default :printf("输入有误,请重新输入\n");
			       break;
	 }

   }
}
void shellsort(struct student a[n],int m)
{
	int i,d,j;
	d=m/2;
	while(d>0)
	{
		for(i=d+1;i<=m;i++)
		{
			a[0]=a[i];
			j=i-d;
			while(j>=0 && a[0].score<a[j].score)
			{
				a[j+d]=a[j];
				j=j-d;
			}
	    a[j+d]=a[0];
		}
		d=d/2;
	}
}
void quicksort(struct student a[n],int low,int high)
{
	int i,j;
	struct student t;
	i=low;
	j=high;
	t=a[low];
	while(i<j)
	{
		while(i<j && a[j].score>t.score)
			j--;
		if(i<j)
			a[i++]=a[j];
		while(i<j && a[i].score<=t.score)
			i++;
		if(i<j)
			a[j--]=a[i];
	}
	a[i]=t;
	if(low<i-1) quicksort(a,low,i-1);
	if(high>i+1) quicksort(a,i+1,high);
}
void bubblesort(struct student a[n],int m)
{
	int i,j,flag;
	for(i=1;i<=m;i++)
	{
		flag=1;
		for(j=1;j<=m-i;j++)
			if(a[j+1].score<a[j].score)
			{
				flag=0;
				a[0]=a[j];
				a[j]=a[j+1];
				a[j+1]=a[0];
			}
			if(flag)
				break;
	}
}
void print(struct student a[n])
{
	int i,k;	
	printf("名次---姓名---成绩\n");
	for(i=n-1;i>=1;i--)
	{
		if(a[i].score==a[i+1].score)
		{
          
			printf("%d,%s,%d",k,a[i].name,a[i].score);
		    printf("\n");
			
		}
		else
		{
		k=n-i;
		printf("%d,%s,%d",k,a[i].name,a[i].score);
		printf("\n");
		}
	}
}
坚_持 2012-11-14
  • 打赏
  • 举报
回复
在结构体里面判断 哪些地方需要交换 然后交换了不就得了
newtee 2012-11-14
  • 打赏
  • 举报
回复
如果排序的过程中要交换关键字的值,那不是其他成员的值也要交换? 可以直接交换结构体
JiMoKuangXiangQu 2012-11-14
  • 打赏
  • 举报
回复
用结构体的指针数组来排序就好了,交换的时候只交换指针就可以了. 如: #define N 20 struct my_struct { int i; char chars[100]; float f; } structs[N]; struct my_struct *pointers[N]; int i; for (i = 0; i < ; i++) pointers[i] = &structs[i]; 比较时遇到要交换时,交换数组pointers的里面的指针就行了. 个人意见,仅供参考.
yellowhwb 2012-11-14
  • 打赏
  • 举报
回复
你用结构体的地址来作为对比的句柄,就不用交换结构体的值了
转角天边 2012-11-14
  • 打赏
  • 举报
回复
不需要,只要交换在比较的那两个关键字的值
yuxi201261 2012-11-14
  • 打赏
  • 举报
回复
结构体的交换可以整体进行交换,交换的时候成员的值是交换了的

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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