当前位置:网站首页>Quick sort function in C language -- qsort
Quick sort function in C language -- qsort
2022-07-06 03:35:00 【Changersh】
Function name
qsort();
The header file
#include<stdlib,h>
function
Quick sort the elements in the array in ascending order by default
Function declaration
void qsort( void *buf, size_t num, size_t size, int (*compare)(const void *, const void *) );
//eg
// Take ascending order
int compare(const void* a, const void* b) {
int* s1 = (int*)a;
int* s2 = (int*)b;
return *s1 - *s2;
}
int n = 9;
int arr[n];
// Suppose that all the data in the array is input
qsort(arr, n, sizeof(int), compare);
buf Is the name of the array to be sorted , It can be an array of characters , integer array , Array of structs … As long as you can compare the size
num Is the number of array elements
size Is the number of bytes occupied by a single element , for example int yes sizeof(int)
compare Is a function that compares sizes , The return value is int, You need to write it yourself
int compare(const void a, const void b)
a and b All are void The variable of , It's a weak type , The system doesn't know what they point to , So when using, you should first convert the type to the required pointer type *
If the function compare The first parameter of is less than the second parameter , Returns a negative value ; Return zero if equal to ; If greater than return positive . Function pair buf The data pointed to is sorted in ascending order .
边栏推荐
- Esbuild & SWC: a new generation of construction tools
- 施努卡:视觉定位系统 视觉定位系统的工作原理
- NR modulation 1
- Pytorch基础——(1)张量(tensor)的初始化
- 3857墨卡托坐标系转换为4326 (WGS84)经纬度坐标
- Analyze menu analysis
- resulttype和resultmap的区别和应用场景
- An article about liquid template engine
- Record the process of reverse task manager
- [slam] orb-slam3 parsing - track () (3)
猜你喜欢
Image super-resolution using deep convolutional networks(SRCNN)解读与实现
JS music online playback plug-in vsplayaudio js
Performance test method of bank core business system
【SLAM】lidar-camera外参标定(港大MarsLab)无需二维码标定板
Buuctf question brushing notes - [geek challenge 2019] easysql 1
[slam] orb-slam3 parsing - track () (3)
Microsoft Research, UIUC & Google research | antagonistic training actor critic based on offline training reinforcement learning
Teach you to build your own simple BP neural network with pytoch (take iris data set as an example)
Image super resolution using deep revolutionary networks (srcnn) interpretation and Implementation
2.1 rtthread pin device details
随机推荐
遥感图像超分辨率论文推荐
给新人工程师组员的建议
Research on cooperative control of industrial robots
1、工程新建
BUAA喜鹊筑巢
MySQL Server层四个日志
深入刨析的指针(题解)
How to write compile scripts compatible with arm and x86 (Makefile, cmakelists.txt, shell script)
【paddle】加载模型权重后预测报错AttributeError: ‘Model‘ object has no attribute ‘_place‘
A brief introduction to symbols and link libraries in C language
[slam] orb-slam3 parsing - track () (3)
StrError & PERROR use yyds dry inventory
Map sorts according to the key value (ascending plus descending)
IPv6 comprehensive experiment
C language circular statement
SD card reports an error "error -110 whilst initializing SD card
【Rust 笔记】18-宏
Suggestions for new engineer team members
Shell 传递参数
[concept] Web basic concept cognition