当前位置:网站首页>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 2)---Basic Chapter (setting and display, serial transmission)
- 高效插入map数据
- The application of Meta analysis in the field of ecological environment
- Real-time waveform display of CAN communication data based on QT (serial eight) ==== "Sub function or new class calls ui control"
- 你不知道的JS语法篇笔记
- vs编译boost库脚本
- 逻辑右移和算术右移区别
- 【江科大自化协stm32F103c8t6】笔记之【入门32单片机及EXTI外部中断初始化参数配置】
- Antd 树拖拽一些细节,官网没有,摸坑篇
- 边境的悍匪—机器学习实战:第二章 端到端的机器学习项目
猜你喜欢
随机推荐
关于map对key自定义排序
Map making of environmental impact assessment based on remote sensing interpretation and GIS technology (the latest guidelines)
QT weekly skills (3)~~~~~~~~~ serial port addition
FPGA parsing B code----serial 2
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Introduction to 32 MCUs and Using TIM Output to Compare and Configure PWM]
QT串口动态实时显示大量数据波形曲线(四)========“界面的美化与处理”
主机和从机配置,建立ssh连接实现Rviz远程控制
>>> /deep/ ::v-deep 深度作用选择器
QT serialization 1: readyRead() function, the solution to incomplete data subcontracting
CPU的三种工作模式:实模式、保护模式、长模式
sizeof和strlen最全区别,以及指针和数组运算解析
C语言,库函数中qsort的用法,及解释
华秋电子成为开放原子开源基金会openDACS捐赠人,共建 openDACS开源生态
xxx is not in the sudoers file.This incident will be reported错误
ssh 脚本 空格字符转换
二进制到汇编:进制,原码反码补码,位运算,通用寄存器,内存一套打通
OpenCV中(rows,cols)与图像(x,y)
QT每周技巧(3)~~~~~~~~~串口添加
pdf和word等文档添加水印
超详细的PCB高可靠辨别方法