当前位置:网站首页>C语言,库函数中qsort的用法,及解释
C语言,库函数中qsort的用法,及解释
2022-07-30 05:46:00 【#唐解元】

C语言库函数qsort的使用
qsort简介
- 大家都知道冒泡排序,但是却排序有一定的局限性,而对较大型的数组而言,函数qsort就派上用场了,它“快速排序”方法是最有效的排序算法之一。
原理:
它把数组不断分成更小的数组,直到变成单元素数组。首先把数组分成两个部分,一部分的值都小于另一部分的值。这个过程一直持续到数组完全排序好为止。
函数原形及逐步分析
一:格式
函数原形:void qsort(void * base ,size_t nmemb, size_t size , int ( * compar)(const void *,const void *));
二:分段解析

看上面的格式可知,这个函数中一共有四个参数:
第一个参数:
是一个指针,指向的是待排序数组的首元素,显得易见base被强制类型转换成指向void的指针,所以,qsort()的第一个实参可以引用任何类型的数组。
———————————————————————————————————————
第二个参数:
第二个参数是待排序项的数量。
———————————————————————————————————————
第三个参数:
因为第一个参数中,把第一个参数转换为void指针,qsort并不知道数组中每个元素的大小,因此第三个参数弥补这个缺陷,第三个参数就是待排序数据中一个元素的大小。
———————————————————————————————————————
第四个参数:
第四个参数是一个指针函数的指针,这个指针指向的比较函数用于确定排序的顺序。
通俗点来说使用来比较待排序数据中的两个元素的函数。
如果第一项的值大于第二项,则返回正数;如果相等就返回0;若小于,就返回第负数。
代码示例
先复习一下经典冒泡排序
输出:0, 1, 2, 3, 4, 5, 6, 7, 8, 9
#include<stdio.h>
#include<string.h>
void bublle_sort(int arr[], int sz)//形参arr本质是指针
{
//确认趟数
int i = 0;
for (i = 0; i < sz - 1; i++)
{
//一趟冒泡排序的过程
int j = 0;
for (j = 0; j < sz - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
//交换
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
int main()
{
int arr[] = {
9,8,7,6,5,4,3,2,1,0 };
int sz = sizeof(arr) / sizeof(arr[0]);//计算数组元素个数
bublle_sort(arr, sz);//数组传参的时候,传递的其实是数组首元素的地址
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
这样的话就用qsort来实现一下冒泡排序
输出:0, 1, 2, 3, 4, 5, 6, 7, 8, 9
#include<stdlib.h>
#include<stdio.h>
void print_arr(int arr[], int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int cmp_int(const void* e1, const void* e2)
{
return *(int*)e1 - *(int*)e2;
}
int main()
{
int arr[10] = {
9,8,7,6,5,4,3,2,1,0};
int sz = sizeof(arr) / sizeof(arr[0]);
qsort(arr, sz, sizeof(arr[0]), cmp_int);
print_arr(arr, sz);
return 0;
}
总结
上面详细的讲到了qsort函数的格式,原理解析,以及代码示例,看似很复杂,但是实用性广,可以实现字符串,结构体内容排序等。
边栏推荐
- Kunlun State Screen Production (serialization 4) --- Basics (graphical setting and display, button lights)
- Generalized Focal Loss paper reading notes
- User password encryption using Bcrypt instead of MD5, SHA1 and SHA256
- Target detection, object classification and semantic segmentation of UAV remote sensing images based on PyTorch deep learning
- R language application in the field of ecological environment
- 多层板的层数,为啥选项都是偶数?就不能选奇数?
- vs compile boost library script
- VsCode与Sublime编辑器优缺点对比
- 逻辑右移和算术右移区别
- How does MATLAB display nii file slice information in the image?
猜你喜欢

jvm之逃逸分析

QT串口动态实时显示大量数据波形曲线(五)========“最终完美解决版”

Kunlun State Screen Production (Serialization 2)---Basic Chapter (setting and display, serial transmission)

Cannnot download sources不能下载源码百分百超详细解决方案
![[Jiangsu University Automation Association stm32F103c8t6] Notes [Initial 32 MCU and EXTI External Interrupt Initialization Parameter Configuration]](/img/e5/87cf293ac3d0c613864e99a8fe9a47.png)
[Jiangsu University Automation Association stm32F103c8t6] Notes [Initial 32 MCU and EXTI External Interrupt Initialization Parameter Configuration]

VSCode隐藏左边活动栏

三种内核结构---宏内核、微内核、混合内核

TCP为什么要三次握手,握手过程中丢包会怎么样?

FPGA解析B码----连载2

Diwen serial screen production (serialization 1) ===== preparation work
随机推荐
闭包(你不知道的JS)
删除当前路径下含某个关键字的所有文件
华秋第八届硬创大赛携手NVIDIA初创加速计划,赋能企业发展
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Entry 32 MCU and GPIO initialization parameter configuration]
js 替换字符串中所有 “ 引号 —— 数据处理
Self-augmented Unpaired Image Dehazing via Density and Depth Decomposition program running record
【江科大自化协stm32F103c8t6】笔记之【入门32单片机及利用TIM输出比较配置PWM】
关于 PCB 多层板制程能力不得不说的那些事儿
干货:线上下单应知应会,快来了解下
jvm之方法区
OpenLayers (ol包),Vite显示地图(附源码)
QT serial 3: LORA test platform based on QT and STM32H750 (2)
VsCode打开终端的方法
openssl1.1.1ARM双编译
逻辑右移和算术右移区别
Target detection, object classification and semantic segmentation of UAV remote sensing images based on PyTorch deep learning
Analysis of domestic data exchange platforms
User password encryption using Bcrypt instead of MD5, SHA1 and SHA256
Generalized Focal Loss paper reading notes
openssl 1.1.1编译语句