qsort 函数的使用

一、qsort说明

qsort是默认升序排列的

使用时要包含头文件   <stdlib.h>

二、用qsort排序数组 

#include<stdlib.h>
#include<stdio.h>
	int compare(const void* elem1, const void* elem2);  // 自定义比较函数声明
	int main()
	{
		int arr[] = { 3,4,6,33,2,79,87,56,7,45 };
		int sz = sizeof(arr) / sizeof(arr[0]);
		qsort(arr, sz, sizeof(int), compare);         //qsort使用
		for (int i = 0; i < sz; i++)
		{
			printf("%d ", arr[i]);
		}
		return 0;
	}
  int compare(const void* elem1, const void* elem2)           //elem1和elem2是 Void *类型,如果直接解引用,无法正常操作
	{
	  return  *((int*)elem1) - *((int*)elem2);         //要把elem1和elem2强制类型转换成int*,再接引用
	}                                             //elem1>elem2  返回>0,    elem1 == elem2  返回0        elem1<elem2  返回<0

三、qsort排序结构体数组

1.以结构体的得分为排序标准

#include<stdlib.h>
#include<stdio.h>
	struct student
	{
		char name[23];
		float score;
		int age;
	};
	int compare_by_stu_score(const void* elem1, const void* elem2);  //声明自定义比较score得分函数

	int main()
	{
		struct student stu[3] = { {"fengcheng",78.4,18},{"zhouzisong",98.4,20},{"zhouzihao",67.9,19} };
		printf("姓名\t        成绩\t年龄\t\n");
		printf("排序前\n");
		for (int i = 0; i < 3; i++)
		{
			printf("%s\t%.2f\t%d\t\n", stu[i].name, stu[i].score, stu[i].age);
		}
		int sz1 = sizeof(stu) / sizeof(stu[0]);         //求结构体数组的元素个数
		int sz2 = sizeof( stu[0]);                 //计算每个元素的字节大小
		qsort(stu, sz1, sz2, compare_by_stu_score);
		printf("排序后\n");
		for (int i = 0; i < 3; i++)
		{
			printf("%s\t%.2f\t%d\t\n", stu[i].name, stu[i].score, stu[i].age);
		}
		return 0;
	}
	int compare_by_stu_score(const void* elem1, const void* elem2)  //自定义比较score得分函数
	{
		return (   (  (struct student*)elem1  )->score - (   (struct student*)elem2)->score    );
		    //强制类型转换成结构体指针
	}

结果:

2.以结构体的姓名为排序标准

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
struct student
{
	char name[23];
	double score;
	int age;
};
int  compare_by_stu_name(const void* elem1, const void* elem2);  //声明自定义比较score得分函数
int main()
{
	struct student stu[3] = { {"fengcheng",78.4,18},{"zhouzisong",98.4,20},{"zhouzihao",67.9,19} };
	int sz1 = sizeof(stu) / sizeof(stu[0]);         //求结构体数组的元素个数
	int sz2 = sizeof(stu[0]);                 //计算每个元素的字节大小
			printf("姓名\t        成绩\t年龄\t\n");
		printf("排序前\n");
		for (int i = 0; i < 3; i++)
		{
			printf("%s\t%.2f\t%d\t\n", stu[i].name, stu[i].score, stu[i].age);
		}
	qsort(stu, sz1, sz2, compare_by_stu_name);
			printf("排序后\n");
		for (int i = 0; i < 3; i++)
		{
			printf("%s\t%.2f\t%d\t\n", stu[i].name, stu[i].score, stu[i].age);
		}
	return 0;
}
int  compare_by_stu_name(const void* elem1, const void* elem2)  //自定义比较score得分函数
{
	return strcmp(((struct student*)elem1)->name, ((struct student*)elem2)->name);
}

结果: 

注意:

所以 strcmp函数的返回值刚好满足

他的头文件是  <string.h>

 四、qsort是默认升序

如需降序排列

只要将return的那里elem1    和   elem2   换个位置,  其他不变

序结果如下: 

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值