100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > c语言快速排序代码不用函数 C语言 快速排序函数

c语言快速排序代码不用函数 C语言 快速排序函数

时间:2020-04-16 18:18:04

相关推荐

c语言快速排序代码不用函数 C语言 快速排序函数

C语言 快速排序函数用法

#include

#include

#include

struct student

{

int id;

char name[12];

char sex;

};

int compare(const void* a,const void* b)//基本数据类型排序

{

return *(char*)a-*(char*)b;//从小到大

//取值//强转为相应类型的指针!!

}

int compare_struct(const void* a,const void* b)

{

return (*(struct student*)a).id-((struct student*)b)->id;

//注意优先级诶!//否则报错在非结构体中。。。

}

int compare_struct_duoji(const void* a,const void* b)//多级排序

{

struct student student_a=*(struct student*)a;

struct student student_b=*(struct student*)b;

if(student_a.id==student_b.id)

{

return student_a.sex-student_b.sex;

}

else

{

return student_a.id-student_b.id;

}

}

void main()

{

//*************char型*************

char a[5]="hello";

qsort(a,5,sizeof(a[0]),compare);

//元素个数//元素大小//函数指针

int i;

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

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

printf("\n");

//************struct型************

struct student e[4]={{100,"chen",'m'},{100,"li",'f'}, \

{70,"wang",'f'},{100,"zhang",'m'}};

qsort(e,4,sizeof(e[1]),compare_struct_duoji);

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

printf("%d %s %c\n",e[i].id,e[i].name,e[i].sex);

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。