当前位置:网站首页>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边栏推荐
- Mysql:insert date:sql error [1292] [22001]: data truncation: incorrect date value:
- Dynamic programming 01 knapsack and complete knapsack
- Exercise 8-7 string sorting
- Print. JS -- web page file printing
- FPGA测试方法以Mentor工具为例
- How to promote the progress of project collaboration | community essay solicitation
- Redis:字符串類型數據的操作命令
- QT learning 22 layout manager (I)
- Using registered classes to realize specific type matching function template
- JS download files through URL links
猜你喜欢

Scroll detection, so that the content in the lower right corner is not displayed at the top of the page, but is displayed as the mouse slides

Fabric. JS document

Example analysis of QT learning 18 login dialog box

FPGA test method takes mentor tool as an example

28:第三章:开发通行证服务:11:在配置文件中定义属性,然后在代码中去获取;

QT learning 19 standard dialog box in QT (top)

Qt学习24 布局管理器(三)

Spring cup eight school league

JS first summary

Redis: redis data structure and key operation commands
随机推荐
消息订阅与发布
交联环糊精金属有机骨架负载甲氨蝶呤缓释微粒|金属-有机多孔材料UiO-66负载黄酮苷类药物|齐岳
How to use lxml to judge whether the website announcement is updated
Canvas utility library fabric JS user manual
信创产业现状、分析与预测
How to delete an attribute or method of an object
Redis:Redis的数据结构、key的操作命令
concat和concat_ws()区别及group_concat()和repeat()函数的使用
JVM family - overview, program counter day1-1
Implementation of Muduo asynchronous logging
How to promote the progress of project collaboration | community essay solicitation
7-8 overspeed judgment
[acnoi2022] guess numbers
Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
simpleParallax. JS (create poor visual effects for website pictures)
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.)
Failure of vector insertion element iterator in STL
Implementation of Muduo accept connection, disconnection and sending data
Interface for querying IP home
selenium 浏览器(1)