C语言快排函数qsort()

原本以为C语言中的快排函数都要靠自己去实现,今天才知道,原来qsort就是C语言中的快排函数,包含在stdlib.h头文件中,函数一共有四个参数,没有返回值。

//int (*cmp)(const void *,const void *);
qsort(*s, n, sizeof(s[0]), cmp);
其中第一个参数s是一个地址,即参与排序的首地址;
n是需要排序的数量;
sizeof(s[0])则是每一个元素占用的空间大小;
指向函数的指针,用于确定排序的顺序。

简单的说:对一个长为1000的数组进行排序时,int a[1000]; 
qsort(a, 1000, sizeof(int), cmp);
//其中cmp函数应写为:
int cmp(const void *a, const void *b)
{
    return *(int*)a - *(int*)b; //由小到大排序
    //return *(int *)b - *(int *)a; 由大到小排序
}
cmp函数的返回值,<0(不进行置换),>0(进行置换),0(不进行置换)。

首先,我们先来手动实现一下快排。

#include <stdio.h>

int a[100], n, temp;

void QuickSort(int h, int t)
{
     if(h >= t) 
         return;
     int mid = (h + t) / 2, i = h, j = t, x;
     x = a[mid];
     while(1)
     {
         while(a[i] < x)
             i++;
         while(a[j] > x) 
             j--;
         if(i >= j) 
             break;
         temp = a[i];
         a[i] = a[j];
         a[j] = temp;
     }
     a[j] = x;
     QuickSort(h, j - 1);
     QuickSort(j + 1, t);
     return ;
}

int main()
{
     int i;
     scanf("%d", &n);
     for(i = 0; i < n; i++)
         scanf("%d", &a[i]);
     QuickSort(0, n - 1);
     for(i = 0; i < n; i++) 
         printf("%d ", a[i]);
     return 0;
}

接着,是对int型数组进行快排。

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

int s[10000], n, i;

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

int main()
{
     scanf("%d", &n);
     for(i = 0; i < n; i++) 
         scanf("%d", &s[i]);

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

     for(i = 0; i < n; i++) 
         printf("%d ", s[i]);

     return 0;
}

double型。

#include <stdio.h>
#include <stdlib.h>
double s[1000];
int i, n;

int cmp(const void * a, const void * b)
{
     return((*(double*)a - *(double*)b>0)?1:-1);
}

int main()
{
     scanf("%d", &n);
     for(i = 0; i < n; i++) 
         scanf("%lf", &s[i]);

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

     for(i = 0; i < n; i++) 
         printf("%lf ", s[i]);

     return 0;
}

char型。

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

char s[10000], i, n;
int cmp(const void *a,const void *b)
{
     return (*(char *)a - *(char *)b);
}

int main()
{
     scanf("%s", s);
     n = strlen(s);
     qsort(s, n, sizeof(s[0]), cmp);

     printf("%s", s);
     return(0);
}

struct型。

#include <stdio.h>
#include <stdlib.h>
struct node
{
     double date1;
     int no;
} s[100];
int i, n;
int cmp(const void *a,const void *b)
{
     struct node *aa = (struct node *)a;
     struct node *bb = (struct node *)b;
     return (((aa->date1) > (bb->date1)) ? 1 : -1);
}

int main()
{
     scanf("%d", &n);
     for(i = 0; i < n; i++)
     {
         s[i].no = i + 1;
         scanf("%lf", &s[i].date1);
     }
     qsort(s, n, sizeof(s[0]), cmp);

     for(i = 0; i < n; i++)
         printf("%d %lf\n", s[i].no, s[i].date1);

     return(0);
}

对结构体排序,加入no来使其稳定(即data值相等的情况下按原来的顺序排)。

#include <stdio.h>
#include <stdlib.h>
struct node
{
     double date1;
     int no;
} s[100];

int i, n;
int cmp(const void *a, const void *b)
{
     struct node *aa = (struct node *)a;
     struct node *bb = (struct node *)b;
     if(aa->date1 != bb->date1)
         return(((aa->date1) > (bb->date1)) ? 1 : -1);
     else
         return((aa->no) - (bb->no));
}
int main()
{
     scanf("%d", &n);
     for(i = 0; i < n; i++)
     {
         s[i].no = i + 1;
         scanf("%lf", &s[i].date1);
     }
     qsort(s, n, sizeof(s[0]), cmp);
     for(i = 0; i < n; i++) 
         printf("%d %lf\n", s[i].no, s[i].date1);
     return 0;
}

对字符串数组的排序(char s[][]型)。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char s[100][100];
int i, n;
int cmp(const void *a, const void *b)
{
     return (strcmp((char*)a, (char*)b));
}

int main()
{
     scanf("%d", &n);
     for(i = 0; i < n; i++) 
         scanf("%s", s[i]);

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

     for(i = 0; i < n; i++)
         printf("%s\n", s[i]);

     return 0;
}

对字符串数组排序(char *s[]型)。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *s[100];
int i, n;
int cmp(const void *a, const void *b)
{
     return (strcmp(*(char**)a, *(char**)b));
}

int main()
{
     scanf("%d", &n);
     for(i = 0; i < n; i++)
     {
         s[i] = (char*)malloc(sizeof(char*));
         scanf("%s", s[i]);
     }
     qsort(s, n, sizeof(s[0]), cmp);
     for(i = 0; i < n; i++) 
         printf("%s\n", s[i]);
     return 0;
}

大致就这样吧,收集了不少资料,看了不少博客才总结在一起了。

全部评论

相关推荐

6月down后继续尝试~【intro】我是UCL(qs&nbsp;top&nbsp;10)城市空间科学硕士,本科是211机械设计制造及自动化(有工科逻辑底子👩🏻‍💻)过去几年,我的经历有点“跨界”,但核心一直围绕着&nbsp;数据分析&nbsp;+&nbsp;空间信息&nbsp;+&nbsp;可持续发展。📍林火遥感监测的研究(发表Remote&nbsp;Sensing论文);📍在浙大某实验室和关联企业中做过与数字孪生、碳排放评估相关的项目,参与一些算法和技术文件的编写。📍python/GIS研究伦敦超低排放区政策(ULEZ)对空气质量的影响;看起来跨度有些大,我其实一直在寻找同一个方向——用数据与空间技术理解和优化真实世界。(🔎详情CV哦)【认真碎碎念】今年6月后迫于求职环境压力,我申请了部分PhD(ESG、城市交通排放、碳中和方向♻️),期间主要在充实研究能力、读文献📄、和导师🧑‍🏫沟通,也因此有一段“空窗期”,希望遇到【不介意】我处于探索发展路径的伯乐呀(福利:面试官还有机会解锁这位&nbsp;理工+人文混血体&nbsp;的有趣副业经历👾)。【意向岗位/城市】希望寻找一份能结合我背景和「兴趣」的工作。意象方向:🌍&nbsp;GIS&nbsp;/&nbsp;遥感&nbsp;/&nbsp;城市数据分析🏙️&nbsp;智慧城市、可持续发展研究🌱&nbsp;碳中和、环境数据分析、ESG政策研究(感兴趣也正学习ing)💡&nbsp;技术与策略结合的岗位,如数据顾问、其他科技方向的项目助理|解决方案|科研研究助理等等意向地点:上海&nbsp;/&nbsp;深圳&nbsp;/香港(接受Hybrid或部分远程)。希望能加入一个包容多元复合型背景、愿意给年轻人自我学习自我成长机会的团队,不介意我“跨界”的路径,更看重逻辑能力、学习力和独立思考的硬实力和软实力。
牛客96914146...:最美的牛客女孩
你觉得哪一届的校招最难?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务