qsort()排序函数

n  排序是最常用的预处理技术

n  qsort包含在<stdlib.h>头文件中

n  函数根据你给的比较条件进行快速排序

n  排序之后的结果仍然放在原数组中

n  使用qsort函数必须自己写一个比较函数。

一、对整型数组排序

Int main()

{

  int a[10]={4,2,7,3,6,1,5};

  int i,n=7;

  qsort(a, n,sizeof(a[0]),cmp);

  for(i=0;i<n;i++)

      printf(“%d “,a[i]);

}

int cmp ( const void *a , const void *b )
{
int *c=(int *)a; //(int*)
是强制类型转换

Int *d=(int *)b;

return *c-*d;

}

等同于return *(int *)a - *(int *)b;

二、对char类型数组排序(同int类型)

n  Int main()

n  {

n    char str[10]=“gjlfjsder”;

n    int i,n;

n    n=strlen(str);

n    qsort(a, n,sizeof(a[0]),cmp);

n    puts(str);

n  }

n  int cmp ( const void *a , const void *b )
{
char *c=(char *)a; //(char*)
是强制类型转换

n  char *d=(char *)b;

n  return *c-*b;

n  }

n   

等同于return *(char *)a - *(char *)b;

三、对double类型数组排序(同int类型)

n  Int main()

n  {

n    double a[10]={5, 2.5, 1.3, 3.2};

n    int i,n=4;

n    qsort(a, n,sizeof(a[0]),cmp);

n    for(i=0;i<n;i++)

        printf(“%.2f “,a[i]);}

int cmp ( const void *a , const void *b )
{
double *c
*d;

c=(double *)a;

d=(double *)b;

return *c>*d ? 1 : -1; //不是return *c-*d

}

等同于return *(double*)a > *(double *)b ? 1:-1;

四、对结构体数组一级排序

n  struct student
{
char name[10];
int score;
}stu[100];

n  int cmp( const void *a ,const void *b)

n  {
struct student *c, *d;

n  c=(struct student *)a;

n  d=(struct student *)b;

n  return  d->score – c->score;   //降序

n  }

n  调用:qsort(stu, 100, sizeof(stu[0]),cmp)

五、对结构体数组二级排序

按照成绩的值从大到小排序,

成绩相同者按姓名从小到大排序

n  struct student
{
char name[10];
int score;
}stu[100];

n  int cmp( const void *a ,const void *b)

n  {
struct student *c, *d;

n  c=(struct student *)a;

n  d=(struct student *)b;

n  if(d->score!=c->score)

n     return  d->score – c->score;   //降序

n  else  //成绩相同,按姓名升序

n     return strcmp(c->name, d->name);

n  }

n  调用:qsort(stu, 100, sizeof(stu[0]),cmp)

`qsort` 函数是 C 语言标准库中的一个通用排序函数,它可以用来对数组进行排序。当需要对结构体数组进行排序时,可以通过传递一个比较函数来指定排序的依据。比较函数是用户自定义的,它按照结构体中的某个字段的值来进行比较。 下面是一个使用 `qsort` 对结构体数组进行排序的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义一个结构体 typedef struct { int id; char name[50]; } Student; // 比较函数,根据结构体中的 id 字段进行比较 int compare_by_id(const void *a, const void *b) { const Student *studentA = (const Student *)a; const Student *studentB = (const Student *)b; return studentA->id - studentB->id; } int main() { // 创建并初始化一个结构体数组 Student students[] = { {3, "Alice"}, {1, "Bob"}, {2, "Charlie"} }; int array_size = sizeof(students) / sizeof(students[0]); // 使用 qsort 和比较函数进行排序 qsort(students, array_size, sizeof(Student), compare_by_id); // 打印排序后的结果 for(int i = 0; i < array_size; i++) { printf("ID: %d, Name: %s\n", students[i].id, students[i].name); } return 0; } ``` 在这个示例中,`compare_by_id` 函数按照结构体中的 `id` 字段来比较两个 `Student` 结构体的大小。然后使用 `qsort` 函数对 `students` 数组进行排序,其中指定了数组的长度、每个元素的大小以及比较函数。 使用 `qsort` 对结构体数组进行排序时,需要特别注意比较函数的编写和元素大小的正确传递,这样才能保证排序的正确性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值