当前位置:网站首页>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函数的格式,原理解析,以及代码示例,看似很复杂,但是实用性广,可以实现字符串,结构体内容排序等。
边栏推荐
- xxx is not in the sudoers file.This incident will be reported错误
- Basic application of XMLBean
- About map custom sorting of keys
- ssh 脚本 空格字符转换
- Difference between logical shift right and arithmetic right shift
- Antd 树拖拽一些细节,官网没有,摸坑篇
- 你不知道的JS思考题
- QT serial 2: LORA test platform based on QT and STM32H750 (1)
- [Jiangsu University of Science and Technology Automation Association stm32F103c8t6] Notes [Initial 32 MCU and TIM timing interrupt initialization parameter configuration]
- 一文盘点五款 BLDC 风机参考方案,建议先马
猜你喜欢

jvm之方法区

QT连载1:readyRead()函数,数据分包不完整解决办法

Antd简单启动一个企业级项目

Kunlun State Screen Production (serialization 4) --- Basics (graphical setting and display, button lights)

干货 | 什么是FOC?一文带你看BLDC电机驱动芯片及解决方案

Atmospheric particulate matter PMF source analysis

Real-time waveform display of CAN communication data based on QT (serial eight) ==== "Sub function or new class calls ui control"

边境的悍匪—Kaggle—泰坦尼克号生还预测详细教程

i++与 ++i 的区别

TCP建立连接的过程
随机推荐
PCB 一分钟科普之你真的懂多层板吗?
QT连载4:基于QT和STM32H750的LORA试验平台(3)
JS的值和引用,复制和传递
你不知道的JS语法篇笔记
服务器基础知识:包含基本概念,作用,服务器选择,服务器管理等(学习来自米拓建站)
你不知道的JS思考题
Real-time waveform display of CAN communication data based on QT (serial eight) ==== "Sub function or new class calls ui control"
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Entry 32 MCU and GPIO initialization parameter configuration]
Cannnot download sources不能下载源码百分百超详细解决方案
BLDC电机应用持续火爆,“网红神器”筋膜枪前景几何?
VsCode与Sublime编辑器优缺点对比
【正点原子】IIC的学习与使用(未完...)
边境的悍匪—机器学习实战:第二章 端到端的机器学习项目
xxx is not in the sudoers file.This incident will be reported错误
User password encryption using Bcrypt instead of MD5, SHA1 and SHA256
如何开发出成功的硬件产品,一个产品由概念的产生到产品的落地量产又需要经历哪些流程呢?
联影医疗二面
干货:线上下单应知应会,快来了解下
Antd简单启动一个企业级项目
Three working modes of CPU: real mode, protected mode, long mode