当前位置:网站首页>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边栏推荐
- QT learning 25 layout manager (4)
- How to bold text in AI
- 7-10 calculate salary
- Canvas utility library fabric JS user manual
- Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content
- FPGA测试方法以Mentor工具为例
- Rasp implementation of PHP
- JS general form submission 1-onsubmit
- Installation impression notes
- Article content typesetting and code highlighting
猜你喜欢

Exercise 6-2 using functions to sum special A-string sequences

7-7 12-24 hour system

allegro,orcad, net alias,port,off-page connector之间的异同点和如何选取

Exercise 8-7 string sorting

Failure of vector insertion element iterator in STL

QT learning 19 standard dialog box in QT (top)

Configure stylelint

QT learning 25 layout manager (4)

QT learning 22 layout manager (I)

Metal organic framework MOFs loaded with non steroidal anti-inflammatory drugs | zif-8 wrapped Prussian blue loaded quercetin (preparation method)
随机推荐
Using registered classes to realize specific type matching function template
FPGA测试方法以Mentor工具为例
UiO-66-COOH装载苯达莫司汀|羟基磷灰石( HA) 包裹MIL-53(Fe)纳米粒子|装载黄芩苷锰基金属有机骨架材料
Redis:字符串类型数据的操作命令
JVM垃圾回收机
Global event bus
Use and design of Muduo buffer class
Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
Redis:字符串類型數據的操作命令
叶酸修饰的金属-有机骨架(ZIF-8)载黄芩苷|金属有机骨架复合磁性材料([email protected])|制备路线
Redis: operation command of string type data
GoLand 2021.1.1: configure the multi line display of the tab of the open file
Invalid Z-index problem
7-10 calculate salary
Qt学习25 布局管理器(四)
Uniapp tips - scrolling components
关于回溯问题中的排列问题的思考(LeetCode46题与47题)
QT learning 22 layout manager (I)
Qt学习22 布局管理器(一)
Fabric. JS document