引用#include<stdlib.h>头文件

qsort()括号里面有4个参数
第一个参数是将要排序的数组名array;
第二个参数是将要排序的数量n;
第三个参数是每个要排序的参数的大小xizeof(array[o]);
第四个参数是自己写的一个比较函数comp;

若排序的是int类型的数组

升序:

int comp(const void*a,const void*b)//用来做比较的函数。
{
    return *(int*)a - *(int*)b;
}

降序:

int comp(const void*a,const void*b)//用来做比较的函数。
{
    return *(int*)b - *(int*)a; //降序
}

若排序的是结构体数组,假定结构体node

typedef struct{
    int a;
int b;

}node;

qsort(a,n,sizeof(node),comp)

 

int comp(const void*x, const void*y)
{
    return ((*(node*)y).a) - ((*(node*))x).a);
}

相关文章:

  • 2022-12-23
  • 2022-01-02
  • 2021-09-11
  • 2021-12-06
  • 2022-12-23
  • 2021-11-17
猜你喜欢
  • 2021-12-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2023-02-14
  • 2022-12-23
  • 2021-09-03
相关资源
相似解决方案