排序(qsort sort的使用)

前情:因平常写代码是常将比较函数弄混(写好了排序还要确认一下-.-!),还是写篇博客,方便以后查阅

C语言qsort函数int类型数组排序:

 1 #include "stdio.h"

 2 #include "stdlib.h"

 3 #define N 1005

 4 int a[N];

 5 

 6 //此比较函数让数组从小到大排列

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

 8 

 9 int main()

10 {

11     int i,n;

12     scanf("%d",&n);

13     for(i=0; i<n; i++) scanf("%d",&a[i]);

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

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

16     printf("\n");

17     return 0;

18 }
View Code

C++中sort函数int类型数组排序:

 1 #include "cstdio"

 2 #include "algorithm"

 3 using namespace std;

 4 #define N 1005

 5 int a[N];

 6 //sort函数不写比较函数的话,默认是从小到大排序的

 7 

 8 int main()

 9 {

10     int i,n;

11     scanf("%d",&n);

12     for(i=0; i<n; i++) scanf("%d",&a[i]);

13     sort(a,a+n); //将地址从a开始到a+n(不包括地址a+n对应元素)的所有数据进行排序

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

15     printf("\n");

16     return 0;

17 }
View Code

C语言qsort函数char类型数组排序:

 1 #include "stdio.h"

 2 #include "string.h"

 3 #include "stdlib.h"

 4 #define N 1005

 5 char str[N];

 6 

 7 //此比较函数让数组从小到大排列

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

 9 

10 int main()

11 {

12     int len;

13     scanf("%s",str);

14     len = strlen(str);

15     qsort(str,len,sizeof(str[0]),cmp);

16     puts(str);

17     return 0;

18 }
View Code

C++中sort函数char类型数组排序:

 1 #include "cstdio"

 2 #include "cstring"

 3 #include "algorithm"

 4 using namespace std;

 5 #define N 1005

 6 char str[N];

 7 

 8 int main()

 9 {

10     int len;

11     scanf("%s",str);

12     len = strlen(str);

13     sort(str,str+len);  //默认也是按ascll码从小到大排序

14     puts(str);

15     return 0;

16 }
View Code

C语言qsort函数double类型数组排序:

 1 #include "stdio.h"

 2 #include "string.h"

 3 #include "stdlib.h"

 4 #define N 1005

 5 double a[N];

 6 

 7 //此比较函数让数组从小到大排列

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

 9 {

10     return *(double *)a > *(double *)b ? 1:-1;

11 }

12 

13 int main()

14 {

15     int i,n;

16     scanf("%d",&n);

17     for(i=0; i<n; i++) scanf("%lf",&a[i]);

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

19     for(i=0; i<n; i++) printf("%.1lf ",a[i]);

20     printf("\n");

21     return 0;

22 }
View Code

C++中sort函数double类型数组排序:

 1 #include "cstdio"

 2 #include "cstring"

 3 #include "algorithm"

 4 using namespace std;

 5 #define N 1005

 6 double a[N];

 7 

 8 //从小到大排序

 9 bool cmp(double a,double b)

10 {

11     return a<b;

12 }

13 

14 int main()

15 {

16     int i,n;

17     scanf("%d",&n);

18     for(i=0; i<n; i++) scanf("%lf",&a[i]);

19     sort(a,a+n,cmp);

20     for(i=0; i<n; i++) printf("%.1lf ",a[i]);

21     printf("\n");

22     return 0;

23 }
View Code

 C语言中qsort函数结构体二级排序:

 1 #include "stdio.h"

 2 #include "string.h"

 3 #include "stdlib.h"

 4 #define N 1005

 5 

 6 typedef struct

 7 {

 8     int x,y;

 9 }Point;

10 Point a[N];

11 //先按结构体中x从小到大排序,x相等的按y从大到小排序

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

13 {

14     Point *c = (Point *)a;

15     Point *d = (Point *)b;

16     if(c->x != d->x)

17         return c->x - d->x;

18     return d->y - c->y;

19 }

20 int main()

21 {

22     int i,n;

23     scanf("%d",&n);

24     for(i=0; i<n; i++) scanf("%d %d",&a[i].x,&a[i].y);

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

26     for(i=0; i<n; i++) printf("%d %d\n",a[i].x,a[i].y);

27     return 0;

28 }
View Code

C++中sort函数结构体二级排序:

 1 #include "cstdio"

 2 #include "cstring"

 3 #include "algorithm"

 4 using namespace std;

 5 #define N 1005

 6 

 7 typedef struct

 8 {

 9     int x,y;

10 }Point;

11 Point a[N];

12 

13 //结构体中按x从小到大排序,x相等的按y从大到小排序

14 bool cmp(Point a,Point b)

15 {

16     if(a.x!=b.x)

17         return a.x < b.x;

18     return a.y > b.y;

19 }

20 

21 int main()

22 {

23     int i,n;

24     scanf("%d",&n);

25     for(i=0; i<n; i++) scanf("%d %d",&a[i].x,&a[i].y);

26     sort(a,a+n,cmp);

27     for(i=0; i<n; i++) printf("%d %d\n",a[i].x,a[i].y);

28     return 0;

29 }
View Code

 C语言qsort函数字符串数组排序:

 1 #include "stdio.h"

 2 #include "string.h"

 3 #include "stdlib.h"

 4 #define N 1005

 5 

 6 typedef struct

 7 {

 8     int data;

 9     char s[N];

10 }TT;

11 

12 TT str[N];

13 //对字符串数组按从小到大排序

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

15 {

16     TT *c = (TT *)a;

17     TT *d = (TT *)b;

18     return strcmp(c->s,d->s); //strcmp(s1,s2),若s1>s2,return 1;若s1<s2,return -1;

19 }

20 

21 int main()

22 {

23     int i,n;

24     scanf("%d",&n);

25     for(i=0; i<n; i++) scanf("%s",str[i].s);

26     qsort(str,n,sizeof(str[0]),cmp);

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

28         puts(str[i].s);

29     return 0;

30 }
View Code

C++中sort函数字符串数组排序:

 1 #include "cstdio"

 2 #include "cstring"

 3 #include "algorithm"

 4 using namespace std;

 5 #define N 1005

 6 

 7 typedef struct

 8 {

 9     int data;

10     char s[N];

11 }TT;

12 

13 TT str[N];

14 //对字符串数组按从小到大排序

15 bool cmp(TT a,TT b) //cmp只返回真或假

16 {

17     return strcmp(a.s,b.s)==-1; //strcmp(s1,s2),若s1>s2,return 1;若s1<s2,return -1;

18 }

19 

20 int main()

21 {

22     int i,n;

23     scanf("%d",&n);

24     for(i=0; i<n; i++) scanf("%s",str[i].s);

25     sort(str,str+n,cmp);

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

27         puts(str[i].s);

28     return 0;

29 }
View Code

 

你可能感兴趣的:(sort)