当前位置:网站首页>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
- 出现Permission denied的解决办法(750权限谨慎使用)
- [Li Kou] the second set of the 280 Li Kou weekly match
- pytorch加载数据
- Pelosi: Congress will soon have legislation against members' stock speculation
- BUAA喜鹊筑巢
- 【SLAM】ORB-SLAM3解析——跟踪Track()(3)
- Map sorts according to the key value (ascending plus descending)
- Idea push rejected solution
- Pytoch foundation - (2) mathematical operation of tensor
猜你喜欢
![[slam] orb-slam3 parsing - track () (3)](/img/87/b580837778c2c9f6bac5ba49403d6b.png)
[slam] orb-slam3 parsing - track () (3)

MADDPG的pythorch实现——(1)OpenAI MADDPG环境配置

Schnuka: visual positioning system working principle of visual positioning system

IPv6 comprehensive experiment

MySQL Server层四个日志

Tidb ecological tools (backup, migration, import / export) collation

1、工程新建

【SLAM】ORB-SLAM3解析——跟踪Track()(3)

1. New project

How to choose PLC and MCU?
随机推荐
Eight super classic pointer interview questions (3000 words in detail)
教你用Pytorch搭建一个自己的简单的BP神经网络( 以iris数据集为例 )
Crawler of explanation and application of agency theory
BUAA magpie nesting
【SLAM】ORB-SLAM3解析——跟踪Track()(3)
Canvas cut blocks game code
Differences and application scenarios between resulttype and resultmap
Lua uses require to load the shared library successfully, but the return is Boolean (always true)
SWC介绍
[slam] orb-slam3 parsing - track () (3)
Recommended foreign websites for programmers to learn
An article about liquid template engine
暑期刷题-Day3
ArabellaCPC 2019(补题)
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Item 10: Prefer scoped enums to unscoped enums.
RT-Thread--Lwip之FTP(2)
Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
Pytorch基础——(2)张量(tensor)的数学运算
Python implementation of maddpg - (1) openai maddpg environment configuration