当前位置:网站首页>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.
边栏推荐
- Kunlun State Screen Production (serialization 4) --- Basics (graphical setting and display, button lights)
- Three working modes of CPU: real mode, protected mode, long mode
- 写在公众号之前——QT,ARM,DSP,单片机,电力电子与传动!
- 边境的悍匪—Kaggle—泰坦尼克号生还预测详细教程
- 【江科大自化协stm32F103c8t6】笔记之【入门32单片机及利用TIM输出比较配置PWM】
- 干货 | 什么是FOC?一文带你看BLDC电机驱动芯片及解决方案
- 探究make_shared效率
- QT serial 3: LORA test platform based on QT and STM32H750 (2)
- 超详细的PCB高可靠辨别方法
- 边境的悍匪—机器学习实战:第三章 分类
猜你喜欢

服务器基础知识:包含基本概念,作用,服务器选择,服务器管理等(学习来自米拓建站)

边境的悍匪—Kaggle—泰坦尼克号生还预测详细教程

Duplicate keys detected:‘/da…‘

探究make_shared效率

R language application in the field of ecological environment

i++与 ++i 的区别

【markdown常用用法】

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

Application of remote sensing, GIS and GPS technology in hydrology, meteorology, disaster, ecology, environment and health

pdf和word等文档添加水印
随机推荐
SSH-RSA密钥
主机和从机配置,建立ssh连接实现Rviz远程控制
MindSpore 提 PR 全流程
求职准备知识点
Anaconda 安装低版本tensorflow
CPU缓存一致性问题
js 替换字符串中所有 “ 引号 —— 数据处理
信号链模拟芯片是什么?
Machine Learning, Deep Learning Based on MATLAB 2021b
OpenCV中(rows,cols)与图像(x,y)
如何判断 PCB 板是否变形?
动态规划进阶 JS
Map making of environmental impact assessment based on remote sensing interpretation and GIS technology (the latest guidelines)
sizeof和strlen最全区别,以及指针和数组运算解析
基于QT的CAN通讯数据实时波形显示(连载八)====“子函数或新类调用ui控件”
2021-09-19 集成学习TASK2
Sklearn : train_test_split()函数的用法
QT weekly skills (3)~~~~~~~~~ serial port addition
租用服务器训练yolov3模型
如何开发出成功的硬件产品,一个产品由概念的产生到产品的落地量产又需要经历哪些流程呢?