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标准库中用于排序和查找的强大工具,通过传递比较函数,我们可以适应不同的数据类型和排序/查找需求。在实际编程中,这种方法能够提高效率,并且是一种常见的编程实践。

相关推荐

  1. C语言使用qsortbsearch实现二分查找

    2023-12-25 03:36:02       40 阅读
  2. c/c++中 qsortbsearch 算法的使用

    2023-12-25 03:36:02       23 阅读
  3. C语言-二分查找

    2023-12-25 03:36:02       24 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-25 03:36:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-25 03:36:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-25 03:36:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-25 03:36:02       18 阅读

热门阅读

  1. C++:第十讲二分查找

    2023-12-25 03:36:02       40 阅读
  2. 7-2 非递归二路归并排序

    2023-12-25 03:36:02       36 阅读
  3. 超酷的爬虫可视化界面

    2023-12-25 03:36:02       37 阅读
  4. C#字典和列表转LuaTable

    2023-12-25 03:36:02       36 阅读
  5. LeeCode前端算法基础100题(15)-最大子数组和

    2023-12-25 03:36:02       31 阅读
  6. leetcode203题目移除链表元素

    2023-12-25 03:36:02       32 阅读
  7. 【.NET Core】反射(Reflection)详解(二)

    2023-12-25 03:36:02       38 阅读
  8. python 安装 Selenium

    2023-12-25 03:36:02       41 阅读
  9. c# opencv 获取多边形中心点

    2023-12-25 03:36:02       38 阅读
  10. ubuntu22.04 安装vscode

    2023-12-25 03:36:02       44 阅读
  11. Unity内置的四种渲染管线简介

    2023-12-25 03:36:02       45 阅读