• leetCode刷题


    C语言qsort()函数的使用

    qsort()函数(quick sort)是八大排序算法中的快速排序,能够排序任意数据类型的数组其中包括整形,浮点型,字符串甚至还有自定义的结构体类型。

    void qsort (void* base, size_t num, size_t size, int (compar)(const void,const void*));

    1.参数含义

    1.首元素地址base
    我们要排序一组数据,首先我们需要找到这组数据在哪,因此我们直接将首元素的地址传给qsort函数来确定从哪开始排序。

    2.元素个数num
    我们知道了从哪开始,也要知道在哪结束才能确定一组需要排序的数据,但是我们不方便直接将结尾元素的地址传入函数,因此我们将需要排序的元素的个数传给qsort函数来确定一组数据。

    3.元素大小size
    我们知道qsort函数能排序任意数据类型的一组数据,因此我们用void类型的指针来接收元素,但是我们知道void类型的指针不能进行加减操作,也就无法移动,那么在函数内部我们究竟用什么类型的指针来操作变量呢?我们可以将void类型的指针强制类型转换成char类型的指针后来操作元素,因为char*类型的指针移动的单位字节长度是1个字节,我们只需要再知道我们需要操作的数据是几个字节就可以操作指针从一个元素移动到下一个元素,因此我们需要将元素大小传入qsort函数。

    4.自定义比较函数compar
    我们需要告诉qsort函数我们希望数据按照怎么的方式进行比较,比如对于几个字符串,我们可以比较字符串的大小(strcmp),也可以比较字符串的长度(strlen),因此我们要告诉qsort函数我们希望的比较方式,我们就需要传入一个比较函数compar就简写为cmp吧。

    2.使用方式
    1.头文件
    要使用qsort函数我们首先需要引用一个头文件

    #include

    3.compar的实现
    qsort函数给cmp函数规定了特定的参数。因此我们设计cmp函数时要严格遵守其参数设定。

    int compar (const void* e1, const void* e2);
    如果你要比较的数据是整形:

    int cmp_int(const void* e1, const void* e2)
    {
    return (int)e1 - (int)e2;
    }
    如果你要比较的数据是浮点型:

    int cmp_float(const void* e1, const void* e2)
    {
    return (int)((float)e1 - (float)e2);
    }
    如果你要比较的是字符串的大小:

    int cmp_str_size(const void* e1, const void* e2)
    {
    return strcmp((char*)e1,(char*)e2);
    }
    如果你要比较的是字符串的长度:

    int cmp_str_len(const void* e1, const void* e2)
    {
    return strlen((char*)e1)-strlen((char*)e2);
    }
    如果你要比较的数据是结构体变量:

    int cmp_by_age(const voide1, const voide2)
    {
    return (int)(((stu*)e1)->weight - ((stu*)e2)->weight);
    }
    需要注意的是:返回结果一定要确保是整形,如果不是一定要强制类型转换成整形!

    整体代码
    快速排序结构体变量示例:

    #include <stdlib.h>
    typedef struct stu
    {
    	//char name;
    	int age;
    	float weight;
    	double hight;
    }stu;
    int cmp_by_age(const void*e1, const void*e2)
    {
    	return (int)(((stu*)e1)->weight - ((stu*)e2)->weight);
    }
    int main()
    {
    	stu class1[3] = { {17,185.5,190.8}, {16,160.9,200.7}, {18,120.3,150.5} };
    	int sz = sizeof(class1) / sizeof(class1[0]);
    	int i;
    	qsort(class1, sz,sizeof(class1[0]), cmp_by_age);
    	for (i = 0; i < 3; i++)
    	{
    		printf("%.1f\n", class1[i].weight);
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    C语言基础:数组(声明数组、初始化数组、访问数组元素)

    1.1 声明数组
    语法:

    // 这叫做一维数组。arraySize 必须是一个大于零的整数常量,
    // type 可以是任意有效的 C 数据类型。

    type arrayName [ arraySize ];
    
    • 1

    示例:例如,要声明一个类型为 double 的包含 10 个元素的数组 balance,声明语句如下。

    double balance[10];
    
    • 1

    1.2 初始化数组
    在 C 中,您可以逐个初始化数组,也可以使用一个初始化语句,如下所示:

    double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
    
    • 1

    下面是一个为数组中某个元素赋值的实例(你可以利用这种方式给数组的每个元素都赋值):

    balance[4] = 50.0;
    
    • 1

    1.3 访问数组元素
    数组元素可以通过数组名称加索引进行访问。元素的索引是放在方括号内,跟在数组名称的后边。例如:把数组中第 10 个元素的值赋给 salary 变量。

    double salary = balance[9];
    
    • 1

    C 库函数 - malloc()

    描述
    C 库函数 void *malloc(size_t size) 分配所需的内存空间,并返回一个指向它的指针。

    声明
    下面是 malloc() 函数的声明: void *malloc(size_t size)
    下面的实例演示了 malloc() 函数的用法。

    实例

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
    int main()
    {
       char *str;
     
       /* 最初的内存分配 */
       str = (char *) malloc(15);
       strcpy(str, "runoob");
       printf("String = %s,  Address = %u\n", str, str);
     
       /* 重新分配内存 */
       str = (char *) realloc(str, 25);
       strcat(str, ".com");
       printf("String = %s,  Address = %u\n", str, str);
     
       free(str);
     
       return(0);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    让我们编译并运行上面的程序,这将产生以下结果:

    String = runoob, Address = 3662685808
    String = runoob.com, Address = 3662685808

  • 相关阅读:
    企业内容管理陷入困境?SaaS工具助力高效解决
    掌握 TypeToken 原理及泛型擦除
    java 同步锁synchronized 解决线程共享数据重复操作问题
    MySQL是如何执行一条SQL更新语句
    FPT(Pretrained Transformer) 2022AAAI
    今日睡眠质量记录79分
    ant的get任务
    HCIA网络课程第三周作业
    1688商品详情接口代码展示
    第22章_瑞萨MCU零基础入门系列教程之DMA控制器
  • 原文地址:https://blog.csdn.net/qianbihua00/article/details/126405218