当前位置:首页 » 编程语言 » c语言结构体冒泡排序
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言结构体冒泡排序

发布时间: 2022-09-18 05:24:52

c语言中结构体数据排序

设结构体名为AAA,结构体数组声明为struct AAA a[N];(N为宏定义常量),身份证成员名为id,则排序函数可如下写——

#include"stdio.h"
#include<string.h>
#defineN3
structAAA{
charid[20];
intage;
};
voidmysort(structAAA*p){//排序函数
structAAAt;
inti,j,k;
for(i=0;i<N;i++){
for(k=i,j=k+1;j<N;j++)
if(strcmp((p+j)->id,(p+k)->id)<0)
k=j;
if(i!=k)
t=*(p+k),*(p+k)=*(p+i),*(p+i)=t;
}
}
intmain(intargc,char*argv[]){//测试主函数
structAAAa[N]={{"650104194812109907",77},{"333018201801015555",1},{"650104194812109903",80}};
mysort(a);
printf("%s %d ",a[0].id,a[0].age);
printf("%s %d ",a[1].id,a[1].age);
printf("%s %d ",a[2].id,a[2].age);
return0;
}

运行结果:

Ⅱ c语言结构体冒泡排序求教

struct Student
{
int ID;
char Name[10];
};

void sort(Student *a, int n)
{
Student t;
int i, j;
for(i=0;i<n;i++)
for(j=0;j<n-i;j++)
if(a[j].ID>a[j+1].ID)
{
memcpy(&t, &a[j], sizeof(Student));
memcpy(&a[j], &a[j+1], sizeof(Student));
memcpy(&a[j+1], &t, sizeof(Student));
}
}
在结构体赋值中,上面使用memcpy函数,如:
memcpy(&t, &a[j], sizeof(Student));
可以替换为:
t.ID = a[j].ID;
strcpy(t.Name, a[j].Name);
依次类推。

Ⅲ C语言,结构体冒泡排序,按平均分由大到小排列,怎么弄啊

给你答案希望采纳!!!!!!
#include <iostream>
#include<vector>
using namespace std;

struct Student
{
const char* name; //名字
float averageScore; //平均分

};

int main()
{
Student one = { "张三",65.2f };
Student two = { "李四",95.5f };
Student three = { "王二",75.8f };
Student four = { "麻子",88.3f };
Student five = { "提问者",5.0f };

//用数组
Student all[] = { one,two,three,four,five };
int count = 5; //5个学生

cout << "排序之前:\n";
for (int i = 0; i < count; ++i)
cout << all[i].name << "的平均成绩为:" << all[i].averageScore << "分\n";
cout << "\n\n\n";

for (int i = 0; i < count - 1; ++i)
{
for (int j = count - 1; j > i; --j)
{
if (all[j].averageScore <= all[j - 1].averageScore)continue;
Student temp = all[j];
all[j] = all[j - 1];
all[j - 1] = temp;
}
}

//测试
cout << "排序之后:\n";
for (int i = 0; i < count; ++i)
cout << all[i].name << "的平均成绩为:" << all[i].averageScore << "分\n";
cout << "\n\n\n";

/*
//用数据结构
vector<Student> allStudents;
allStudents.push_back(one);
allStudents.push_back(two);
allStudents.push_back(three);
allStudents.push_back(four);
allStudents.push_back(five);

for (int i = 0; i < allStudents.size() - 1; ++i)
{
for (int j = allStudents.size() - 1; j > i; --j)
{
if (allStudents[j].averageScore <= allStudents[j - 1].averageScore)continue;
Student temp = allStudents[j];
allStudents[j] = allStudents[j - 1];
allStudents[j - 1] = temp;
}
}

*/

return 1;
}

Ⅳ C语言冒泡排序。

#include<stdio.h>

void main()

{

int a[10];

int i,j,t;

printf("input 10 numbers: ");

for(i=0;i<10;i++)

scanf("%d",&a[i]);

for(j=0;j<9;j++) /*进行9次循环 实现9趟比较*/

for(i=0;i<9-j;i++) /*在每一趟中进行9-j次比较*/

if(a[i]>a[i+1]) /*相邻两个数比较,想降序只要改成a[i]<a[i+1]*/

{

t=a[i];

a[i]=a[i+1];

a[i+1]=t;

}

printf("the sorted numbers: ");

for(i=0;i<10;i++)

printf(" %d",a[i]);


}

(4)c语言结构体冒泡排序扩展阅读:

冒泡排序算法的运作

1、比较相邻的元素。如果第一个比第二个大(小),就交换他们两个。

2、对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。这步做完后,最后的元素会是最大(小)的数。

3、针对所有的元素重复以上的步骤,除了最后已经选出的元素(有序)。

4、持续每次对越来越少的元素(无序元素)重复上面的步骤,直到没有任何一对数字需要比较,则序列最终有序。

简单的表示

#include <stdio.h>

void swap(int *i, int *j)

{

int temp = *i;

*i = *j;

*j = temp;

}

int main()

{

int a[10] = {2,1,4,5,6,9,7,8,7,7};

int i,j;

for (i = 0; i < 10; i++)

{

for (j = 9; j > i; j--)//从后往前冒泡

{

if (a[j] < a[j-1])

{

swap(&a[j], &a[j-1]);

}

}

}

for (i = 0; i < 10; i++)

{

printf("%d ", a[i]);

}

return 0;

}

参考资料来源:冒泡排序-网络

Ⅳ C语言结构体用冒泡排序法计算结构体中的一个成员,并输出排序后的结构体

for(j=0;j<3;j++)

改为for(j=0;j<2;j++)

for(i=0;i<3-j;i++)

改为for(i=1;i<3;i++)
看看

Ⅵ C语言结构体冒泡排序交换

j没有赋值
for循环第二行
j没有赋值

Ⅶ C语言结构体排序

#include<stdio.h>

struct
{
long long id;
char name[32];
int s;
}t,stus[200];

int main()
{
int n,i,swap=1,sorted=0;

scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%lld %s %d",&stus[i].id,stus[i].name,&stus[i].s);
printf("\n");
while(swap)
{
swap=0;
for(i=0;i<n-1-sorted;i++)
{
if(stus[i+1].s>stus[i].s)
{
swap=1;
t=stus[i];
stus[i]=stus[i+1];
stus[i+1]=t;
}
}
sorted++;
}
for(i=0;i<n;i++)
printf("%lld %s %d\n",stus[i].id,stus[i].name,stus[i].s);
return 0;
}

Ⅷ C语言结构体+冒泡排序,大神帮我改改把它编完吧

#include<stdio.h>
#include<string.h>
#include<stdlib.h>


structstudent
{
charname[20];
intid;
floatscore;
};
voidsort(student*a,intn)
{
studentt;
inti,j;
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j].score<a[j+1].score)
{
memcpy(&t,&a[j],sizeof(student));
memcpy(&a[j],&a[j+1],sizeof(student));
memcpy(&a[j+1],&t,sizeof(student));
}
}
}
for(i=0;i<n;++i)
{
printf("学生成绩排名:%s%d%f ",a[i].name,a[i].id,a[i].score);
}
}
intmain(void)
{
inti,n;
structstudent*m;
printf("请输入学生人数:");
scanf("%d",&n);
m=(structstudent*)malloc(n*sizeof(student));
for(i=0;i<n;i++)
{
printf("请输入第%d个学生姓名学号成绩:",i+1);
scanf("%s%d%f",m[i].name,&m[i].id,&m[i].score);
}
sort(m,n);
}

请输入第0个学生姓名学号成绩:wgewg 2 33

输入的时候用空格分开

Ⅸ C语言,用结构体编写冒泡排序

struct Student
{
int ID;
char Name[10];
};

void sort(Student *a, int n)
{
Student t;
int i, j;
for(i=0;i<n;i++)
for(j=0;j<n-i;j++)
if(a[j].ID>a[j+1].ID)
{
memcpy(&t, &a[j], sizeof(Student));
memcpy(&a[j], &a[j+1], sizeof(Student));
memcpy(&a[j+1], &t, sizeof(Student));
}
}
在结构体赋值中,上面使用memcpy函数,如:
memcpy(&t, &a[j], sizeof(Student));
可以替换为:
t.ID = a[j].ID;
strcpy(t.Name, a[j].Name);
依次类推。