当前位置:网站首页>【C语言】qsort函数
【C语言】qsort函数
2022-07-06 05:58:00 【SouLinya】
前言
qsort函数是八大排序算法中的快速排序,能够排序任意数据类型的数组,包括实现整型,浮点型,结构体,字符串等的排序。
一、qsort函数原型
头文件:#include<stdio.h>
void qsort(void* base,//要排序的数据的首元素地址
int num,//要排序的数据个数
int width,//元素的字节大小
void(*cmp)(const void* e1, const void* e2));//函数指针-接收比较函数,比较函数需要我们自己实现
关于为什么要使用void*指针:首先要明确void**是无具体类型的指针,可以接受任意类型的地址,而qsort函数可以排序多种类型的数据,所以我们利用空指针接受地址。需要注意的是空指针没有确定的步长大小,所以空指针不能加减整数,不能对空指针进行解引用。
二、
1.qsort函数实现整型排序
(1)升序
#include<stdio.h>
#include<stdlib.h>
//整型的比较函数
int cmp_int(const void* e1, const void* e2)
{
return *(int*)e1 - *(int*)e2;
}
int main()
{
int arr[] = {
9,8,7,6,5,4,3,2,1,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
//qsort库函数的使用
qsort(arr, sz, sizeof(arr[0]), cmp_int);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
(2)降序
#include<stdio.h>
#include<stdlib.h>
int cmp_int(const void* e1, const void* e2)
{
return *(int*)e2 - *(int*)e1;//后一个元素比前一个元素大,就交换
}
int main()
{
int arr[] = {
0,1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr[0]);
qsort(arr, sz, sizeof(arr[0]), cmp_int);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
2.qsort函数实现结构体排序
(1)按年龄排序
#include<stdio.h>
#include<stdlib.h>
//定义结构体类型
struct Stu
{
//结构体成员
int age;
char name[20];
};
int cmp_stu_by_age(const void* e1, const void* e2)
{
//需要e1需要括号括起,才能访问到结构体数据
return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}
int main()
{
struct Stu s[] = {
{
30,"lisi"},{
20,"wangwu"},{
10,"zhangsan"} };
int sz = sizeof(s) / sizeof(s[0]);
qsort(s, sz, sizeof(s[0]), cmp_stu_by_age);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d %s\n", s[i].age, s[i].name);
}
return 0;
}
(2)按名字排序
#include<stdio.h>
#include<stdlib.h>
struct Stu
{
int age;
char name[20];
};
int cmp_stu_by_name(const void* e1, const void* e2)
{
//字符串的比较用strcmp
return strcmp( ((struct Stu*)e1)->name, ((struct Stu*)e2)->name );
}
int main()
{
struct Stu s[] = {
{
30,"lisi"},{
20,"wangwu"},{
10,"zhangsan"} };
int sz = sizeof(s) / sizeof(s[0]);
qsort(s, sz, sizeof(s[0]), cmp_stu_by_name);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d %s\n", s[i].age, s[i].name);
}
return 0;
}
三. 冒泡排序算法模拟实现qsort函数
(1)排序整数
#include<stdio.h>
//比较函数
int cmp_int(const void* e1, const void* e2)
{
return *(int*)e1 - *(int*)e2;
}
//以字节单位交换
void Swap(char* buf1, char* buf2, int width)//以字节为单位交换
{
int i = 0;
for (i = 0; i < width; i++)
{
char temp = *buf1;
*buf1 = *buf2;
*buf2 = temp;
buf1++;
buf2++;
}
}
void bubble_sort(char* base, int sz, int width, int(*cmp)(int, int))
{
int i = 0;
int j = 0;
int flag = 0;
for (i = 0; i < sz - 1; i++)
{
flag = 1;
for (j = 0; j < sz - 1 - i; j++)
{
//强制类型转换成char*类型
if (cmp_int((char*)base+j*width,(char*)base+(j+1)*width)>0)
{
Swap((char*)base + j * width, (char*)base + (j + 1)*width, width);
flag = 0;
}
}
if (flag == 1)
{
break;
}
}
}
int main()
{
int arr[] = {
1,3,5,7,9,2,4,6,8,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, sz, sizeof(arr[0]), cmp_int);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
(2)排序浮点数
#include<stdio.h>
float cmp_float(const void* e1, const void* e2)
{
return *(float*)e1 - *(float*)e2;
}
void Swap(char* buf1, char* buf2, int width)//以字节为单位交换
{
int i = 0;
for (i = 0; i < width; i++)
{
char temp = *buf1;
*buf1 = *buf2;
*buf2 = temp;
buf1++;
buf2++;
}
}
void bubble_sort(void* base, int sz, int width, int(*cmp)(void*, void*))
{
int i = 0;
int j = 0;
int flag = 0;
for (i = 0; i < sz - 1; i++)
{
flag = 1;
for (j = 0; j < sz - 1 - i; j++)
{
if (cmp_float((char*)base+j*width,(char*)base+(j+1)*width)>0)
{
Swap((char*)base + j * width, (char*)base + (j + 1)*width, width);
flag = 0;
}
}
if (flag == 1)
{
break;
}
}
}
int main()
{
float arr[] = {
9.0,8.0,7.0,6.0,5.0,4.0,3.0,1.1,1.0 };
int sz = sizeof(arr) / sizeof(arr[0]);
int i = 0;
bubble_sort(arr, sz, sizeof(arr[0]), cmp_float);
for (i = 0; i < sz; i++)
{
printf("%.1f ", arr[i]);
}
return 0;
}
边栏推荐
- 华为BFD的配置规范
- 进程和线程
- Construction of yolox based on paste framework
- Title 1093: character reverse order
- [experience] install Visio on win11
- [Thesis code] SML part code reading
- Bit operation rules
- Pay attention to the details of pytoch code, and it is easy to make mistakes
- 如何在业务代码中使用 ThinkPHP5.1 封装的容器内反射方法
- J'ai un chaton.
猜你喜欢
![[web security] nodejs prototype chain pollution analysis](/img/c5/256ab30e796f0859387585873cee8b.jpg)
[web security] nodejs prototype chain pollution analysis

如何在业务代码中使用 ThinkPHP5.1 封装的容器内反射方法

Grant Yu, build a web page you want from 0

First knowledge database

关于 PHP 启动 MongoDb 找不到指定模块问题

How to use the container reflection method encapsulated by thinkphp5.1 in business code

C language bubble sort

Sqlmap tutorial (III) practical skills II

MPLS test report
![[Jiudu OJ 08] simple search x](/img/a7/12a00c5d1db2deb064ff5f2e83dc58.jpg)
[Jiudu OJ 08] simple search x
随机推荐
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
HCIA复习
Seven imperceptible truths in software testing
Luogu [Beginner Level 4] array p1427 number game of small fish
Baidu online AI competition - image processing challenge: the 8th program of handwriting erasure
【论文代码】SML部分代码阅读
H3C防火墙RBM+VRRP 组网配置
AUTOSAR从入门到精通番外篇(十)-嵌入式S19文件解析
Is it difficult for an information system project manager?
Report on market depth analysis and future trend prediction of China's arsenic trioxide industry from 2022 to 2028
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Winter 2021 pat class B problem solution (C language)
Li Chuang EDA learning notes 12: common PCB board layout constraint principles
A master in the field of software architecture -- Reading Notes of the beauty of Architecture
LAN communication process in the same network segment
Node 之 nvm 下载、安装、使用,以及node 、nrm 的相关使用
Mysql database master-slave cluster construction
查詢生產訂單中某個(些)工作中心對應的標准文本碼
[string] palindrome string of codeup
Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai