当前位置:网站首页>C library function - qsort()
C library function - qsort()
2022-07-03 14:10:00 【Big fish】
describe
C Library function void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*)) Sort the array .
Statement
Here is qsort() Declaration of functions .
void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))
Parameters
base: Array to sort
nmemb: The number of elements in the array
size: Each array element takes up memory space , You can use sizeof get
compar: A comparison function that compares two array elements . The value of the first parameter of this comparison function is less than 、 be equal to 、 When it is greater than the value of the second parameter , The return values of this comparison function should be less than 、 be equal to 、 Greater than zero .That is to say, you need to implement such a function ( Ascending ):
int cmp(const void *a, const void *b)
If a > b, return >0
If a == b, return 0
If a < b, return <0
there a and b The relationship between is only logical , It's not worth comparison , So sorting can be more than numbers , It can also be characters .
Return value
This function does not return any value .
example
The following example demonstrates qsort() Function usage .
example
#include <stdio.h>
#include <stdlib.h>
int values[] = { 88, 56, 100, 2, 25 };
int cmpfunc (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main()
{
int n;
printf(" Sort the previous list :\n");
for( n = 0 ; n < 5; n++ ) {
printf("%d ", values[n]);
}
qsort(values, 5, sizeof(int), cmpfunc);
printf("\n Sorted list :\n");
for( n = 0 ; n < 5; n++ ) {
printf("%d ", values[n]);
}
return(0);
}
Let's compile and run the above program , This will produce the following results :
Sort the previous list :
88 56 100 2 25
Sorted list :
2 25 56 88 100
边栏推荐
- GoLand 2021.1.1: configure the multi line display of the tab of the open file
- Common network state detection and analysis tools
- JS new challenges
- Article content typesetting and code highlighting
- Function calling convention
- MySQL data processing value addition, deletion and modification
- jvm-对象生命周期
- Page generation QR code
- Mysql:insert date:sql error [1292] [22001]: data truncation: incorrect date value:
- QT learning 17 dialog box and its types
猜你喜欢
Uniapp tips - scrolling components
Uio-66-cooh loaded bendamostine | hydroxyapatite (HA) coated MIL-53 (FE) nanoparticles | baicalin loaded manganese based metal organic skeleton material
Generate directories from web content
The small project (servlet+jsp+mysql+el+jstl) completes a servlet with login function, with the operation of adding, deleting, modifying and querying. Realize login authentication, prevent illegal log
Implementation of Muduo accept connection, disconnection and sending data
Spring cup eight school league
jvm-运行时数据区
How to use lxml to judge whether the website announcement is updated
Cross linked cyclodextrin metal organic framework loaded methotrexate slow-release particles | metal organic porous material uio-66 loaded with flavonoid glycosides | Qiyue
Use and design of Muduo buffer class
随机推荐
js 2023. String pair equal to the target string after connection
Use vscode to view hex or UTF-8 codes
玖逸云黑免费无加密版本源码
Redis:字符串类型数据的操作命令
Solve the problem of dormitory router campus network sharing login
Toast UI editor (editor allows you to edit your markup document using text or WYSIWYG, with syntax highlighting, scrolling synchronization, real-time preview and chart functions.)
28: Chapter 3: develop Passport Service: 11: define attributes in the configuration file, and then obtain them in the code;
Solve MySQL 1045 access denied for user 'root' @ 'localhost' (using password: yes)
Implementation of Muduo accept connection, disconnection and sending data
JVM family - overview, program counter day1-1
7-9 find a small ball with a balance
jvm-运行时数据区
如何使用lxml判断网站公告是否更新
[ACNOI2022]猜数
JVM runtime data area
Spring cup eight school league
Common plug-ins for vite project development
Exercise 7-6 count capital consonants
Configure stylelint
JS shift operators (< <,> > and > > >)