C语言使用qsort和bsearch实现二分查找

引言

在计算机科学领域,查找是一项基本操作,而二分查找是一种高效的查找算法。本博客将详细解释一个简单的C语言程序,演示如何使用标准库函数qsortbsearch来对一个整数数组进行排序和二分查找。

代码解析

包含头文件

#include <stdio.h>
#include <stdlib.h>

首先,我们包含了两个标准头文件,stdio.h用于输入输出操作,stdlib.h用于内存分配和其他一些杂项功能。

比较函数

int compareIntegers(const void *a, const void *b) {
    return *(int *)a - *(int *)b;
}

定义了一个比较函数compareIntegers,该函数用于在排序和二分查找时比较两个整数。这个函数的作用是返回a - b的值,即升序排序。

主函数

int main() {
    // 创建已排序的整数数组
    int numbers[] = {101, 305, 248, 407, 109};
    int numNumbers = sizeof(numbers) / sizeof(numbers[0]);

    // 排序数组
    qsort(numbers, numNumbers, sizeof(numbers[0]), compareIntegers);

    // 设置要查找的 number
    int targetNumber = 305;

    // 使用bsearch搜索学生
    int *result = (int *)bsearch(&targetNumber, numbers, numNumbers, sizeof(numbers[0]), compareIntegers);

    // 检查结果并输出
    if (result != NULL) {
        printf("Number found: %d\n", *result);
    } else {
        printf("Number %d not found\n", targetNumber);
    }

    return 0;
}
创建并排序数组
int numbers[] = {101, 305, 248, 407, 109};
int numNumbers = sizeof(numbers) / sizeof(numbers[0]);
qsort(numbers, numNumbers, sizeof(numbers[0]), compareIntegers);

在主函数中,我们首先创建了一个整数数组numbers,然后使用sizeof操作符计算数组元素个数。接下来,我们使用qsort函数对数组进行升序排序,传递了比较函数compareIntegers来定义排序顺序。

二分查找
int targetNumber = 305;
int *result = (int *)bsearch(&targetNumber, numbers, numNumbers, sizeof(numbers[0]), compareIntegers);

设置要查找的目标数字为305,然后使用bsearch函数在已排序的数组中进行二分查找。同样,我们传递了比较函数compareIntegers来确保查找的一致性。

输出结果
if (result != NULL) {
    printf("Number found: %d\n", *result);
} else {
    printf("Number %d not found\n", targetNumber);
}

最后,我们检查bsearch的结果。如果找到了目标数字,就输出找到的数字;否则,输出未找到的消息。

时间复杂度

让我们分析一下这个程序中排序和查找部分的时间复杂度:

  1. 排序 (qsort):

    • 时间复杂度:O(n * log(n))
      • qsort通常使用快速排序算法,其平均时间复杂度为O(n * log(n)),其中n是数组的元素个数。
      • 在这个程序中,numNumbers是5,所以排序的时间复杂度为O(5 * log(5))。
  2. 查找 (bsearch):

    • 时间复杂度:O(log(n))
      • bsearch使用二分查找算法,其时间复杂度为O(log(n)),其中n是数组的元素个数。
      • 在这个程序中,数组已经排好序,numNumbers是5,所以查找的时间复杂度为O(log(5))。

因此,整个程序的时间复杂度主要由排序的部分决定,为O(5 * log(5))。在大O表示法中,忽略常数项,这可以简化为O(log(5)),即O(1)。因此,总体时间复杂度可以近似看作是O(1),即常数时间。这意味着程序的运行时间与数组的规模无关,对于小规模的数组来说是非常高效的。

总结

这个简单的C程序演示了如何使用qsort对数组进行排序,然后使用bsearch进行二分查找。这两个函数是C标准库中用于排序和查找的强大工具,通过传递比较函数,我们可以适应不同的数据类型和排序/查找需求。在实际编程中,这种方法能够提高效率,并且是一种常见的编程实践。

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

D0ublecl1ck

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值