function
<cstdlib>

qsort

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

各参数:1 待排序数组首地址 2 数组中待排序元素数量 3 各元素的占用空间大小 4 指向函数的指针

Sort elements of array

Sorts the num elements of the array pointed to by base, each element size bytes long, using the compar function to determine the order.

The sorting algorithm used by this function compares pairs of elements by calling the specified compar function with pointers to them as argument.

The function does not return any value, but modifies the content of the array pointed to by base reordering its elements as defined by compar.

The order of equivalent elements is undefined.

Parameters

base
Pointer to the first object of the array to be sorted, converted to a void*.
num
Number of elements in the array pointed to by base.
size_t is an unsigned integral type.
size
Size in bytes of each element in the array.
size_t is an unsigned integral type.
compar
Pointer to a function that compares two elements.
This function is called repeatedly by qsort to compare two elements. It shall follow the following prototype:

 
int compar (const void* p1, const void* p2);
 

Taking two pointers as arguments (both converted to const void*). The function defines the order of the elements by returning (in a stable and transitive manner):

return value meaning
<0 The element pointed to by p1 goes before the element pointed to by p2
0 The element pointed to by p1 is equivalent to the element pointed to by p2
>0 The element pointed to by p1 goes after the element pointed to by p2

For types that can be compared using regular relational operators, a general compar function may look like:

1
2
3
4
5
6
int compareMyType (const void * a, const void * b)
{if ( *(MyType*)a <  *(MyType*)b ) return -1;if ( *(MyType*)a == *(MyType*)b ) return 0;if ( *(MyType*)a >  *(MyType*)b ) return 1;
}
 

Return Value

none

(本文默认采用从小到大排序)

一、对int类型数组排序

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10int cmp(const void *a, const void *b)
{return *((int*)a) - *((int*)b);
}int main()
{srand((unsigned)time(NULL));int num[100];for (int i = 0; i < N; i++){num[i] = rand() % 100;}puts("待排序数组:");for (int i = 0; i < N; i++){printf("%d ", num[i]);}putchar('\n');qsort(num, N, sizeof(num[0]), cmp);puts("排序后:");for (int i = 0; i < N; i++){printf("%d ", num[i]);}
}

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

三、对double类型数组排序

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10int cmp(const void *a, const void *b)
{return *((double*)a) > *((double*)b) ? 1 : -1;
}int main()
{srand((unsigned)time(NULL));double num[100];for (int i = 0; i < N; i++){num[i] = rand() % 100 / 2.0;}puts("待排序数组:");for (int i = 0; i < N; i++){printf("%.1f ", num[i]);}putchar('\n');qsort(num, N, sizeof(num[0]), cmp);puts("排序后:");for (int i = 0; i < N; i++){printf("%.1f ", num[i]);}
}

四、对结构体一级排序

(按照chinese的数值从小到大排序)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10typedef struct SCORE
{int chinese;int math;int english;
}SCORE;int cmp(const void *a, const void *b)
{return ((SCORE*)a)->chinese - ((SCORE*)b)->chinese;
}int main()
{srand((unsigned)time(NULL));SCORE scores[100];for (int i = 0; i < N; i++){scores[i].chinese = rand() % 100;scores[i].math = rand() % 100;scores[i].english = rand() % 100;}puts("待排序数据:");for (int i = 0; i < N; i++){printf("%d %d %d\n", scores[i].chinese, scores[i].math, scores[i].english);}putchar('\n');qsort(scores, N, sizeof(SCORE), cmp);puts("排序后:");for (int i = 0; i < N; i++){printf("%d %d %d\n", scores[i].chinese, scores[i].math, scores[i].english);}
}

五、对结构体二级排序

(按照chinese从小到大排序,相同时按照math从小到大排序)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10typedef struct SCORE
{int chinese;int math;int english;
}SCORE;int cmp(const void *a, const void *b)
{if (((SCORE*)a)->chinese != ((SCORE*)b)->chinese){return ((SCORE*)a)->chinese - ((SCORE*)b)->chinese;}else{return ((SCORE*)a)->math - ((SCORE*)b)->math;}
}int main()
{srand((unsigned)time(NULL));SCORE scores[100];for (int i = 0; i < N; i++){scores[i].chinese = rand() % 100;scores[i].math = rand() % 100;scores[i].english = rand() % 100;}puts("待排序数据:");for (int i = 0; i < N; i++){printf("%d %d %d\n", scores[i].chinese, scores[i].math, scores[i].english);}putchar('\n');qsort(scores, N, sizeof(SCORE), cmp);puts("排序后:");for (int i = 0; i < N; i++){printf("%d %d %d\n", scores[i].chinese, scores[i].math, scores[i].english);}
}

六、对字符串进行排序

(按照name字典序进行排序)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 3typedef struct PERSON
{char name[20];int age;
}PERSON;int cmp(const void *a, const void *b)
{return strcmp(((PERSON*)a)->name, ((PERSON*)b)->name);
}int main()
{PERSON persons[N];strcpy(persons[0].name, "aab");persons[0].age = 10;strcpy(persons[1].name, "abc");persons[1].age = 11;strcpy(persons[2].name, "aaa");persons[2].age = 12;puts("待排序数据:");for (int i = 0; i < N; i++){printf("%s %d\n", persons[i].name, persons[i].age);}putchar('\n');qsort(persons, N, sizeof(PERSON), cmp);puts("排序后:");for (int i = 0; i < N; i++){printf("%s %d\n", persons[i].name, persons[i].age);}
}

转载于:https://www.cnblogs.com/lgh1992314/p/5834709.html

qsort函数的用法相关推荐

  1. 【C语言】重要函数qsort函数的用法

    目录 一.qsort函数的介绍 1.整形数组 2.字符数组 3.字符串 4.结构体 二.qsort函数的使用 一.qsort函数的介绍 qsort函数是一种底层快速排序的函数,它的特点就是可以排序任意 ...

  2. C语言关于qsort函数的用法详细说明

    快速排序是一种用的最多的排序算法,在C语言的标准库中也有快速排序的函数,下面说一下详细用法. qsort函数包含在<stdlib.h>中 qsort函数声明如下: void qsort(v ...

  3. c语言qsort函数的用法与模拟实现

    一.qsort函数说明 首先他是一个库函数,在使用时需要包含stdlib.h这个头文件,其次他是一个基于快速排序算法的排序函数,任何类型的数据都可以实现排序,比如字符串排序,结构体排序及整形排序. 二 ...

  4. sort和qsort函数的用法

    转自:http://blog.csdn.net/gneveek/article/details/7988062 做ACM题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的O(n^2)排 ...

  5. C语言sort和qsort函数的用法

          做ACM题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的O(n^2)排序,不但程序容易超时,而且浪费宝贵的比赛时间,还很有可能写错.STL里面有个sort函数,可以直接对 ...

  6. qsort函数详细讲解,各种用法,妙用

    众所周知,在C++中有STL库可以用来进行排序,而对于新手来说很少知道C语言竟然也有自己的排序函数. qsort(基本快速排序的方法,每次把数组分成两部分和中间的一个划分值,而对于有多个重复值的数组来 ...

  7. C语言 qsort()函数详解 (笔记)

    qsort函数,其声明在stdlib.h文件中,时间复杂度为n*log(n). 功能:使用快速排序例程进行排序 用法: void qsort(void *base, size_t nitems, si ...

  8. 【C语言】详解qsort函数

    目录 一.什么是qsort 1.1.qsort函数的声明 1.2.qsort函数的参数 二.qsort函数的用法 1.1.qsort函数的实例 1.2.测试qsort函数排序结构体数据 1.3.用冒泡 ...

  9. 【C语言】qsort函数用法(转)

    qsort函数用法 qsort 功 能: 使用快速排序例程进行排序 用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(cons ...

最新文章

  1. 简单借还书管理系统c语言,急求程序!!!简单图书馆借/还书管理子系统
  2. 从硬件到框架,30+巨头参与的AI基准竞争结果公布(第一回合)
  3. Android中进程间通讯 AIDL
  4. JavaScript面向对象精要(一)
  5. 【项目管理】敏捷小品:Rupert 工业公司 项目:~Alpha~
  6. javaweb和ajax使用查询出来的数据做下拉菜单_不会用Excel做数据筛选,老板叼的你没话说!...
  7. 【玩转cocos2d-x之三十二】xml的解析
  8. video_replay如何捕获和回放WebRTC视频流
  9. 13 代码分割之import静动态导入
  10. java单元测试算初级_Java____Eclipse下JUnit单元测试(初级)
  11. 求数组第二大元素 和 字符串拷贝的实现
  12. 麻省理工学生令计算机系统升级不需重启
  13. Tricks(二十五)—— decorator(在函数调用前后打印日志)
  14. PLT hook与Inline hook
  15. 濛濛有感——懂与不懂(一)
  16. 泰克示波器入门级TBS1102C+电流探头TCP2020方案
  17. linux下实现文件双向同步 unsion,unison做数据双向同步
  18. [31期]命运掌握在自己手中
  19. 香港服务器 微信支付,如何给微信开通香港钱包(WeChat Pay HK)
  20. Ubuntu20.04设置开机自启脚本、开机自启命令(ubuntu自启,ubuntu开机自启)rc(run command)(systemd)(/etc/rc.local)(开机启动原理)开机自启动

热门文章

  1. 连接到localhost后提示要求用户名和密码
  2. LVS的工作原理和相关算法
  3. IFile、File与实体转换
  4. XmlReader and XmlWriter in .NET
  5. 10.25 es问题
  6. spring boot 下载
  7. AtomicLong可以被原子地读取和写入的底层long值的操作
  8. js实现数独算法(优化版本)
  9. tomcat端口号被占用
  10. Winform DataGridView列的单元格中动态添加图片和文字