c/c++中 qsort 与 bsearch 算法的使用


author: hjjdebug

date: 2023年 12月 13日 星期三 17:30:41 CST

descriptor: qsort & bsearch 算法的使用


qsort 用来排序,bsearch用来搜索,是这个意思,但具体怎样使用呢?

qsort 不仅可以用来排序一个整数数组,还可以排序一个结构数组.例子中给出了使用方法.

它真的会在内存中把数据不断的搬移来完成数据的排序.

如果你觉得排序结构数组比较浪费时间,可以让它排序指针.

qsort 在/usr/include/stdlib.h 中定义,

排序数组基地址为base, 数组元素个数nmemb, 每个元素大小为size, 用__compar来比大小

/* Sort NMEMB elements of BASE, of SIZE bytes each,

using COMPAR to perform the comparisons. */

extern void qsort (void *__base, size_t __nmemb, size_t __size,

__compar_fn_t __compar) __nonnull ((1, 4));

bsearch 在/usr/include/stdlib.h 中定义, 就在qsort 函数的上方

bsearch 二分搜索方法,是需要一个排序的数组的,所以先排序数组,然后让bsearch 搜索key

告诉它比较大小的方法,它就能返回能不能找到这个key了

二进制搜索一个key, 在BASE地址, 元素个数NMEMB, 元素大小SIZE,比较函数__COMPAR

/* Do a binary search for KEY in BASE, which consists of NMEMB elements

of SIZE bytes each, using COMPAR to perform the comparisons. */

extern void *bsearch (const void *__key, const void *__base,

size_t __nmemb, size_t __size, __compar_fn_t __compar)

有了这么好的功能,我们要利用起来, 下面是样例代码:

$ cat main.cpp

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

/*

* 用途: 在命令行输入月份的简写, 它会告诉你是几月.

* 例如: ./bsearch aug , 会输出 8

* 实现方法: 当然是先建立一个表,然后让它找了.

* 由于这个表是我们人类方便查阅的方式给出的,为了编程的需要,先把它按月份名称的ascii顺序排序,

* 然后就可以用二分法来查找这个表了.

* 使用了c库中的qsort 函数, bsearch 函数

*/

//定义一个"月份信息" 结构 month_info, 月份号,月份名称

struct month_info {

int nr;

const char *name;

} months\[\] = {

{ 1, "jan" }, { 2, "feb" }, { 3, "mar" }, { 4, "apr" },

{ 5, "may" }, { 6, "jun" }, { 7, "jul" }, { 8, "aug" },

{ 9, "sep" }, {10, "oct" }, {11, "nov" }, {12, "dec" }

};

#define nr_of_months (sizeof(months)/sizeof(months0))

//"月份信息" 变量"大小"比较, 就是它们的名字比较

static int comp_mi(const void *m1, const void *m2)

{

struct month_info *mi1 = (struct month_info *) m1;

struct month_info *mi2 = (struct month_info *) m2;

return strcmp(mi1->name, mi2->name);

}

int main(int argc, char **argv)

{

printf("before sort:\n");

for(size_t i=0;i<nr_of_months;i++)

{

printf("#%d,%s ",monthsi.nr,monthsi.name);

}

printf("\n");

//按照ascii 顺序把结构数组重新排序

qsort(months, nr_of_months, sizeof(struct month_info), comp_mi);

printf("after sort:\n");

for(size_t i=0;i<nr_of_months;i++)

{

printf("#%d,%s ",monthsi.nr,monthsi.name);

}

printf("\n");

for (int i = 1; i < argc; i++) {

struct month_info key, *res;

key.name = argvi;

res = (struct month_info *)bsearch(&key, &months, nr_of_months, sizeof(struct month_info), comp_mi);

if (res == NULL)

printf("'%s': unknown month\n", argvi);

else

printf("%s: month #%d\n", res->name, res->nr);

}

exit(EXIT_SUCCESS);

}

下面是执行结果:

$ ./bsearch aug jan

before sort:

#1,jan #2,feb #3,mar #4,apr #5,may #6,jun #7,jul #8,aug #9,sep #10,oct #11,nov #12,dec

after sort:

#4,apr #8,aug #12,dec #2,feb #1,jan #7,jul #6,jun #3,mar #5,may #11,nov #10,oct #9,sep

aug: month #8

jan: month #1

相关推荐
一只不想敲代码的程序猿13 小时前
如何更高效地拆解问题?
算法
微尘hjx14 小时前
【Deep-OC-SORT 02】多轨迹算法Deep-OC-SORT-相对 OC-SORT 的改进点
人工智能·算法·yolo·目标跟踪·视觉检测·ultralytics·deep-oc-sort
梦帮科技14 小时前
意识启发式量子隧穿退火算法用于全局优化 (CITA) 完整技术指南
网络·人工智能·算法·量子计算·模拟退火算法
一去不复返的通信er15 小时前
排序 + 双指针:力扣15三数之和
算法·leetcode
Pniubi15 小时前
力扣3无重复字符的最长字串(滑动窗口)
算法·leetcode·职场和发展
On The Road :15 小时前
深入理解 JVM 垃圾回收:GC 是什么、如何判定垃圾、四大回收算法全解析
jvm·算法
微尘hjx16 小时前
【Deep-OC-SORT 03】多轨迹算法Deep-OC-SORT-卡尔曼滤波预测过程
算法·yolo·目标跟踪·ultralytics·车跟踪·轨迹算法·deep-oc-sort
老师我太想进步了202618 小时前
【数据结构-3】算法美学之降复杂度
数据结构·算法
机器人零零壹1 天前
技术白皮书:iRobotCAM如何解决机器人激光切割应用中的核心难点,从CAD离线编程到双目视觉定位与轨迹自适应提升复杂空间曲线切割的精度、效率与柔性化水平
人工智能·算法
ZhengEnCi1 天前
大模型的分类到底怎么分:从基础模型到推理模型、从通用到专用的完整图谱
算法