当前位置:网站首页>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 .
边栏推荐
- [rust notes] 18 macro
- Eight super classic pointer interview questions (3000 words in detail)
- Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
- Polymorphic day02
- Mysql database operation
- 1.16 - 校验码
- IPv6 comprehensive experiment
- 11. Container with the most water
- 简述C语言中的符号和链接库
- Data analysis Seaborn visualization (for personal use)
猜你喜欢
Analyze menu analysis
Performance test method of bank core business system
How to do function test well
Esbuild & SWC: a new generation of construction tools
深入探究指针及指针类型
Idea push rejected solution
MPLS experiment
SAP ALV cell level set color
【SLAM】ORB-SLAM3解析——跟踪Track()(3)
Schnuka: visual positioning system working principle of visual positioning system
随机推荐
【RISC-V】外部中断
canvas切积木小游戏代码
Getting started with applet cloud development - getting user search content
IPv6 comprehensive experiment
Recommended papers on remote sensing image super-resolution
1、工程新建
These are not very good
Image super resolution using deep revolutionary networks (srcnn) interpretation and Implementation
1. New project
Overview of OCR character recognition methods
给新人工程师组员的建议
C language judgment, ternary operation and switch statement usage
three.js网页背景动画液态js特效
Precautions for single chip microcomputer anti reverse connection circuit
[concept] Web basic concept cognition
BUAA计算器(表达式计算-表达式树实现)
StrError & PERROR use yyds dry inventory
Research on cooperative control of industrial robots
简述C语言中的符号和链接库
svg拖动点裁剪图片js特效