当前位置:网站首页>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.
边栏推荐
- survivor区对象何时进入老年代(深入理解jvm中表述不准确的地方)
- 如何判断 PCB 板是否变形?
- QT每周技巧(2)~~~~~~~~~界面按钮
- QT serial and CAN dynamic real-time display the log data
- 【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
- clinit方法
- 昆仑通态屏幕制作(连载3)---基础篇(按钮串口发送)
- 每日一知识:手写深拷贝和浅拷贝(解决了循环引用的问题)
- 服务器基础知识:包含基本概念,作用,服务器选择,服务器管理等(学习来自米拓建站)
- Flood Control Assessment Report Compilation Method and Flood Modelling under the New Guidelines (HEC-RAS)
猜你喜欢
jvm之方法区
jvm之逃逸分析
动态规划入门 JS
QT serial 3: LORA test platform based on QT and STM32H750 (2)
BLDC电机应用持续火爆,“网红神器”筋膜枪前景几何?
查找Proj4js地图投影参数
influxDB运维记录
【正点原子】IIC的学习与使用(未完...)
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Introduction to 32 MCUs and Using TIM Output to Compare and Configure PWM]
Cannnot download sources不能下载源码百分百超详细解决方案
随机推荐
antd table Summary总结栏置顶
闭包(你不知道的JS)
主机和从机配置,建立ssh连接实现Rviz远程控制
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Introduction to 32 MCUs and Using TIM Output to Compare and Configure PWM]
pdf和word等文档添加水印
openssl 1.1.1 compile statement
动态规划进阶 JS
QT serial and CAN dynamic real-time display the log data
干货 | 什么是FOC?一文带你看BLDC电机驱动芯片及解决方案
Through the bit operations to convert the characters are case sensitive
QT weekly skills (2)~~~~~~~~~ interface buttons
About map custom sorting of keys
QT serial 3: LORA test platform based on QT and STM32H750 (2)
QT每周技巧(3)~~~~~~~~~串口添加
与所有 ARM 工具、软件兼容?韦斯佰瑞这款MCU内核值得关注!
二进制到汇编:进制,原码反码补码,位运算,通用寄存器,内存一套打通
2021年软考中级过关
Target detection, object classification and semantic segmentation of UAV remote sensing images based on PyTorch deep learning
C语言,库函数中qsort的用法,及解释
OpenLayers 初学者指南,源码测试可用