C语言快速排序-qsort函数

目录

一.qsort简介

二.qsort详解

1)函数原型

2)快速排序源码

三.使用qsort进行排序

目录

一.qsort简介

二.qsort详解

1)函数原型

2)快速排序源码

三.使用qsort进行排序

1)int型

2)double型

3)char型

4)struct型

5)字符串数组型(按照长度大小)

6)字符串数组型(按照首字母大小)

7)字符型数组(char s[][]型)

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


一.qsort简介

qsort函数是C语言编译器函数库自带的快速排序函数。

其包含在#include<stdlib.h>头文件里面,所以在使用的时候需要加上该头文件。

其本身还是快速排序,所以时间复杂度仍然是O(nlogn)

二.qsort详解

1)函数原型

//int (*cmp)(const void *,const void *); 

qsort(*s, n, sizeof(s[0]), cmp);
//详细
 void qsort(void *base, size_t nitems, size_t size, int (*compare)(const void *, const void*))

 含在stdlib.h头文件中,函数一共有四个参数,没有返回值。

其中第一个参数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; 由大到小排序
}

(int *)a表示将a地址强制类型转换成整形地址类型
*(int*)a 就是得到目标地址的值 

 注意:cmp函数的返回值,<0(不进行置换),>0(进行置换),0(不进行置换)。

2)快速排序源码

#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;
}

详细代码可参考:七大排序算法详解

三.使用qsort进行排序

1)int型

//1.对整数排序
#include<stdio.h>
#include<stdlib.h>
#define MAXN 100010
int n,a[MAXN];

//升序排序
int compare1(const void *a,const void *b)
{
	return *(int*)a-*(int*)b;
}

//降序排序
int compare2(const void *a,const void *b)
{
	return *(int*)b-*(int*)a;
}
int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
	qsort(a,n,sizeof(int),compare1);
	//qsort(a,n,sizeof(int),compare2);
	for(int i=0;i<n;i++)
		printf("%d ",a[i]);
	return 0;
}

2)double型

//2.对double型排序
#include<stdio.h>
#include<stdlib.h>
#define MAXN 100010
int n;
double a[MAXN];

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

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

int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		scanf("%lf",&a[i]);
	qsort(a,n,sizeof(double),compare1);
	for(int i=0;i<n;i++)
		printf("%lf ",a[i]);
	return 0;
}

3)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);
}

4)struct型

#include <stdio.h>
#include <stdlib.h>
struct node
{
     double date1;
     int no;
} s[100];
int i, n;
//对结构体数组中的date1排序
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)));
}

//对no排序
int cmp(const void *a,const void *b)
{
     struct node *aa = (struct node *)a;
     struct node *bb = (struct node *)b;
     return (((aa->no) > (bb->no)));
}
//拓展
//利用三元运算符,将排序顺序改为:
//对结构体排序,加入no来使其稳定(即data值相等的情况下按原来的顺序排)。
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)));
     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);
}

5)字符串数组型(按照长度大小)

//对字符串排序
//按照长度大小排序
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXN 100010
int	 n;
char a[MAXN][MAXN];

int compare1(const void *a, const void *b)
{
	return strlen((char *)a) - strlen((char *)b);
} 

int compare2(const void *a, const void *b)
{
	return strlen((char *)b) - strlen((char *)a);
}

int main()
{
	scanf("%d",&n);
	getchar();		//吃掉换行符;
	for(int i=0;i<n;i++)
		gets(a[i]);
	qsort(a,n,sizeof(char)*MAXN,compare1);
	for(int i=0;i<n;i++)
	printf("%s\n",a[i]);
	return 0;
}

6)字符串数组型(按照首字母大小)

//对字符串排序
//按照首字母大小排序
#include<stdio.h>
#include<stdlib.h>
#define MAXN 100010
int	 n;
char a[MAXN][MAXN];

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

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

int main()
{
	scanf("%d",&n);
	getchar();		//吃掉换行符;
	for(int i=0;i<n;i++)
		gets(a[i]);
	qsort(a,n,sizeof(char)*MAXN,compare1);
	for(int i=0;i<n;i++)
	printf("%s\n",a[i]);
	return 0;
}

7)字符型数组(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;
}

8)字符串数组排序(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;
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值