当前位置:网站首页>C language, usage of qsort in library function, and explanation
C language, usage of qsort in library function, and explanation
2022-07-30 07:24:00 【#tangjieyuan】

C语言库函数qsort的使用
qsort简介
- Everyone knows bubble sort,But sorting has certain limitations,For larger arrays,函数qsort就派上用场了,它“快速排序”方法是最有效的排序算法之一.
原理:
它把数组不断分成更小的数组,直到变成单元素数组.First divide the array into two parts,一部分的值都小于另一部分的值.This process continues until the array is completely sorted.
Function prototype and step-by-step analysis
一:格式
函数原形:void qsort(void * base ,size_t nmemb, size_t size , int ( * compar)(const void *,const void *));
二:分段解析

See the format above,There are four parameters in this function:
第一个参数:
是一个指针,Points to the first element of the array to be sorted,easy to seebaseis cast to pointervoid的指针,所以,qsort()The first argument of can refer to any type of array.
———————————————————————————————————————
第二个参数:
The second parameter is the number of items to be sorted.
———————————————————————————————————————
第三个参数:
because of the first parameter,Convert the first argument to void指针,qsortIt doesn't know the size of each element in the array,So the third parameter makes up for this deficiency,The third parameter is the size of an element in the data to be sorted.
———————————————————————————————————————
第四个参数:
The fourth parameter is a pointer to a pointer function,The comparison function pointed to by this pointer is used to determine the sort order.
In layman's terms a function used to compare two elements in the data to be sorted.
如果第一项的值大于第二项,则返回正数;如果相等就返回0;若小于,It returns the negative number.
代码示例
Let's review the classic bubble sort first
输出:0, 1, 2, 3, 4, 5, 6, 7, 8, 9
#include<stdio.h>
#include<string.h>
void bublle_sort(int arr[], int sz)//形参arr本质是指针
{
//确认趟数
int i = 0;
for (i = 0; i < sz - 1; i++)
{
//一趟冒泡排序的过程
int j = 0;
for (j = 0; j < sz - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
//交换
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
int main()
{
int arr[] = {
9,8,7,6,5,4,3,2,1,0 };
int sz = sizeof(arr) / sizeof(arr[0]);//计算数组元素个数
bublle_sort(arr, sz);//数组传参的时候,传递的其实是数组首元素的地址
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Use it like thatqsortto implement a bubble sort
输出:0, 1, 2, 3, 4, 5, 6, 7, 8, 9
#include<stdlib.h>
#include<stdio.h>
void print_arr(int arr[], int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int cmp_int(const void* e1, const void* e2)
{
return *(int*)e1 - *(int*)e2;
}
int main()
{
int arr[10] = {
9,8,7,6,5,4,3,2,1,0};
int sz = sizeof(arr) / sizeof(arr[0]);
qsort(arr, sz, sizeof(arr[0]), cmp_int);
print_arr(arr, sz);
return 0;
}
总结
mentioned in detail aboveqsort函数的格式,原理解析,以及代码示例,看似很复杂,But it is very practical,可以实现字符串,Structure content sorting, etc.
边栏推荐
- 边境的悍匪—机器学习实战:第三章 分类
- 无法完成包的安装npm ERR! Refusing to install package with name “moment“ under a package also called “moment“
- QT连载1:readyRead()函数,数据分包不完整解决办法
- openssl 1.1.1 compile statement
- CLUE Model Construction Method, Model Validation and Land Use Change Scenario Prediction
- 高交会重要活动之一|2020中国硬件创新大赛全国总决赛
- 【markdown常用用法】
- 【江科大自化协stm32F103c8t6】笔记之【入门32单片机及GPIO初始化参数配置】
- [Quick MSP430f149] Notes on learning MSP430f149 during the game
- 2021年软考中级过关
猜你喜欢

迪文串口屏幕制作(连载一)=====准备工作

昆仑通态屏幕制作(连载5)---基础篇(串口接收,文本与灯显示)

昆仑通态屏幕制作(连载4)---基础篇(图形设定与显示,按钮灯)

QT串口动态实时显示大量数据波形曲线(五)========“最终完美解决版”

FPGA parsing B code----serial 2

边境的悍匪—机器学习实战:第三章 分类

antd table Summary总结栏置顶

创建快捷方式时如何不带“快捷方式“后缀字样?

昆仑通态屏幕制作(连载3)---基础篇(按钮串口发送)
![[Jiangsu University Automation Association stm32F103c8t6] Notes [Initial 32 MCU and EXTI External Interrupt Initialization Parameter Configuration]](/img/e5/87cf293ac3d0c613864e99a8fe9a47.png)
[Jiangsu University Automation Association stm32F103c8t6] Notes [Initial 32 MCU and EXTI External Interrupt Initialization Parameter Configuration]
随机推荐
通过位运算进行字符大小写转换
CLUE Model Construction Method, Model Validation and Land Use Change Scenario Prediction
【正点原子】sys.c、sys.h位带操作的简单应用
昆仑通态屏幕制作(连载4)---基础篇(图形设定与显示,按钮灯)
【江科大自化协stm32F103c8t6】笔记之【入门32单片机及EXTI外部中断初始化参数配置】
关于报错vscode
jvm之方法区
Atmospheric particulate matter PMF source analysis
Antd简单启动一个企业级项目
昆仑通态屏幕制作(连载5)---基础篇(串口接收,文本与灯显示)
QT serial and CAN dynamic real-time display the log data
主机和从机配置,建立ssh连接实现Rviz远程控制
Duplicate keys detected:‘/da…‘
高集成度 MCU 市场增大,如何加速 BLDC 领域落地应用
【Qingdao Station】High-level application of SWAT model and modeling of areas without data, uncertainty analysis and climate change, improvement of land use surface pollution impact model and case analy
爬2.12.6的Antd上传图片坑
QT连载2:基于QT和STM32H750的LORA试验平台(1)
QT连载3:基于QT和STM32H750的LORA试验平台(2)
Kunlun state screen production (serial 3) - based article (button serial port to send)
FPGA parsing B code----serial 2