Sort函数(c)

(来自codeblocks)

stdlib.h

_CRTIMP void __cdecl qsort(void*, size_t, size_t,

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

(来自网络)

void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )

int compare (const void *elem1, const void *elem2 ) );

qsort(quicksort)主要根据你给的比较条件给一个快速排序,主要是通过指针移动实现排序功能。排序之后的结果仍然放在原来数组中。

参数意义如下:

base:需要排序的目标数组开始地址

num:目标数组元素个数

width:目标数组中每一个元素长度

compare:函数指针,指向比较函数

Compare 函数的返回值

描述

< 0

elem1将被排在elem2前面

0

elem1 等于 elem2

> 0

elem1 将被排在elem2后面

知识点:

1.

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

返回值必须为整数(int)

若a<b,返回:

负数:升序

正数:降序

2.

*(long *)a

(long *):指a为(long *)类型

*a:指获得地址为a的存储单元的数据

用于:

目录

1.一维数组... 2

整型:... 2

实型:... 3

2.一维数组(降序) 4

3.二维 struct      也可用于一维,如字符串比较... 4

4.char一维数组... 6

5.char二维数组... 6

字符串中str[0]~str[n-1]的值进行排序... 6

Another Way:... 7

6.注意:... 8

1.一维数组

整型:

#include <stdio.h>

#include <stdlib.h>

#define maxn 100

long a[maxn+1];

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

{

if (*(long *)a<*(long *)b)

return -1;

else

return 1;

//    return (*(long *)a-*(long *)b);

///当数为整型,两种方式都可以

///而当数不为整型时,只能采用最上面的方法

}

int main()

{

long n,i;

scanf("%ld",&n);

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

scanf("%ld",&a[i]);

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

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

printf("%ld ",a[i]);

return 0;

}

/*

10

5 3 2 10 1 6 9 4 8 7

*/

实型:

当使用*(double *)a-*(double *)b

如果两者相差0.1,则return 0.1,返回值为0 [返回的数为整数,0.1处理为0,(long)0.1=0],,但事实上并非如此。

Input:

5

0.3 0.2 0.5 0.1 0.4

Output:

0.20 0.50 0.10 0.40 0.30

输出错误。

#include <stdio.h>

#include <stdlib.h>

#define maxn 100

double a[maxn+1];

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

{

if (*(double *)a<*(double *)b)

return -1;

else

return 1;

//    return (*(double *)a-*(double *)b);

///当数为整型,两种方式都可以

///而当数不为整型时,只能采用最上面的方法

}

int main()

{

long n,i;

scanf("%ld",&n);

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

scanf("%lf",&a[i]);

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

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

printf("%.2lf ",a[i]);

return 0;

}

/*

5

0.3 0.2 0.5 0.1 0.4

*/

2.一维数组(降序)

与升序相比,cmp中输出改为相反数 或 交换a,b

#include <stdio.h>

#include <stdlib.h>

#define maxn 100

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

{

if (*(long *)a<*(long *)b)

return 1;

else

return -1;

}

int main()

{

long n,a[maxn+1],i;

scanf("%ld",&n);

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

scanf("%ld",&a[i]);

qsort(a,n,sizeof(long),cmp);

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

printf("%ld ",a[i]);

return 0;

}

/*

10

5 3 2 10 1 6 9 4 8 7

*/

3.二维 struct        也可用于一维,如字符串比较

#include <stdio.h>

#include <stdlib.h>

#define maxn 100

struct node

{

long x,y;

};

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

{

if ((*(struct node *)a).x<(*(struct node *)b).x)

return -1;

else if ((*(struct node *)a).x>(*(struct node *)b).x)

return 1;

if ((*(struct node *)a).y<(*(struct node *)b).y)

return -1;

else

return 1;

}

int main()

{

struct node info[maxn+1];

long n,i;

scanf("%ld",&n);

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

scanf("%ld%ld",&info[i].x,&info[i].y);

qsort(info,n,sizeof(struct node),cmp);

printf("\n");

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

printf("%ld %ld\n",info[i].x,info[i].y);

return 0;

}

/*

5

1 4

1 2

3 1

3 2

2 1

*/

4.char一维数组

字符串中s[0]~s[len-1]的值进行排序

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define maxlen 100

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

{

return *(char *)a-*(char *)b;

}

int main()

{

long len;

char s[maxlen+1];

gets(s);

len=strlen(s);

qsort(s,len,sizeof(char),cmp);

printf("%s\n",s);

return 0;

}

/*

bdcea

*/

5.char二维数组

字符串中str[0]~str[n-1]的值进行排序

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define maxn 100

#define maxlen 100

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

{

return strcmp((char *)a,(char *)b);

}

int main()

{

//可以 strcmp(char *a,char *b) (char *a) (char *b)

//*a,*b比较;*(a+1),*(b+1)比较;…………

//可以这样使用:a[][],等同于*(a+k) (char *a)

//因为字符从地址a开始依次存储(a,a+1,a+2,……)

//当使用scanf("%s",s); (char s[])

//&s即为char *类型,将字符串存储在&s的地址里(以&s为始,&s=s[0])

//当使用scanf("%s",a); (char *a)

//&a即为&(char *),为char* 的地址,

//char *是存储char类型数据的地址的数据结构,存的是整数(地址)

//所以不能向char *类型(字符的地址)写入数据(字符串)

long n,i;

char str[maxn+1][maxlen+1];

scanf("%ld",&n);

//去除 数n后面的'\n'

gets(str[0]);

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

gets(str[i]);

//sizeof(str[0])=maxlen+1

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

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

printf("%s\n",str[i]);

return 0;

}

/*

3

ac

abcd

abc

*/

Another Way:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define maxn 100

#define maxlen 100

//char *的指针(存储char *类型)为char **类型

//*(char **)a为char *类型

//可以这样使用strcmp(char *,char *)

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

{

return strcmp(*(char **)a,*(char **)b);

}

int main()

{

long n,i;

char str[maxn+1][maxlen+1];

char *pstr[maxlen+1];

scanf("%ld",&n);

gets(str[0]);

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

{

gets(str[i]);

pstr[i]=str[i];

}

qsort(pstr,n,sizeof(char *),cmp);

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

printf("%s\n",pstr[i]);

return 0;

}

/*

3

ac

abcd

abc

*/

6.注意:

可以排序数组一部分的内容

#include <stdio.h>

#include <stdlib.h>

#define maxn 100

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

{

return *(long *)a-*(long *)b;

}

int main()

{

long a[maxn+1],n,x,y,i;

scanf("%ld%ld%ld",&n,&x,&y);

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

scanf("%ld",&a[i]);

//a为数组a的首地址,a+x-1为数组a中下标为x-1的数的存储地址,即a[x-1]的地址

//排序a[x]~a[y]的内容

qsort(a+x-1,y-x+1,sizeof(long),cmp);

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

printf("%ld ",a[i]);

return 0;

}

/*

Input:

5 2 4

5 4 3 2 1

Output:

5 2 3 4 1

*/

/////////////////////////////////////////////////////////////////////////////////////////

Sort函数(c++)

针对不同的排序使用不同的sort

template <class RandomAccessIterator>

void sort ( RandomAccessIterator first, RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>

void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );

sort(x,y)指的是排序从x开始,从y结束(不包括y)的连续的同类型的内容

sort中比较函数返回Boolean类型

 

1.一维数组

#include <iostream>

#include <algorithm>

#define maxn 100

using namespace std;

int main()

{

long n,a[maxn+1],i;

cin>>n;

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

cin>>a[i];

sort(a,a+n);

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

cout<<a[i]<<" ";

return 0;

}

2.一维数组(降序)

#include <iostream>

#include <algorithm>

#define maxn 100

using namespace std;

bool cmp(long a,long b)

{

return a>b;

}

int main()

{

int n,a[maxn+1],i;

cin>>n;

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

cin>>a[i];

sort(a,a+n,cmp);

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

cout<<a[i]<<" ";

return 0;

}

3.二维 struct          也可用于一维,如字符串比较

#include <iostream>

#include <algorithm>

#define maxn 100

using namespace std;

struct node

{

int x,y;

};

bool cmp(struct node a,struct node b)

{

if (a.x<b.x)

return 1;

else if (a.x>b.x)

return 0;

if (a.y<b.y)

return 1;

else

return 0;

}

int main()

{

struct node s[maxn+1];

int n,i;

cin>>n;

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

cin>>s[i].x>>s[i].y;

sort(s,s+n,cmp);

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

cout<<s[i].x<<" "<<s[i].y<<endl;

return 0;

}

/*

5

1 4

1 2

3 1

3 2

2 1

*/

4.char一维数组

#include <iostream>

#include <cstring>

#include <algorithm>

#define maxlen 100

using namespace std;

int main()

{

char s[maxlen];

cin.getline(s,maxlen);

sort(s,s+strlen(s));

cout<<s<<endl;

return 0;

}

5.char二维数组

sort(x,y)指的是排序从x开始,从y结束(不包括y)的连续的同类型的内容

所以不能sort(str,str+n,cmp),因为str[]的长度无法判断。

#include <iostream>

#include <cstring>

#include <algorithm>

#define maxn 100

#define maxlen 100

using namespace std;

bool cmp(char *x,char *y)

{

if (strcmp(x,y)<0)

return true;

else

return false;

}

int main()

{

char str[maxn+1][maxlen+1];

char *pstr[maxlen+1];

long n,i;

cin>>n;

//去除数n后面的'\n'

cin.getline(str[0],maxlen);

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

{

cin.getline(str[i],maxlen);

pstr[i]=str[i];

}

//改变pstr[x],pstr[y]的值,却不会改变pstr[x]的地址和pstr[y]的地址下的内存的内容

/*例子:

地址         内容

1               ‘a’

2               ‘c’

3               ‘\0’

10             ‘a’

11             ‘\0’

起始:             pstr[1]=1;                 pstr[2]=10;

升序排序后:pstr[1]=10;     pstr[2]=1;

*/

sort(pstr,pstr+n,cmp);

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

cout<<pstr[i]<<endl;

return 0;

}

/*

3

ac

abcd

abc

*/

注意:

可以排序数组一部分的内容

#include <iostream>

#include <algorithm>

#define maxn 100

using namespace std;

int main()

{

long n,a[maxn+1],x,y,i;

cin>>n>>x>>y;

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

cin>>a[i];

sort(a+x-1,a+y);

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

cout<<a[i]<<" ";

return 0;

}

/*

Input:

5 2 4

5 4 3 2 1

Output:

5 2 3 4 1

*/

Qsort(c)_Sort(c++)用法的更多相关文章

  1. qsort()与sort的用法(收藏)

    sort()函数是C++中的排序函数其头文件为:#include<algorithm>头文件: qsort()是C中的排序函数,其头文件为:#include<stdlib.h> ...

  2. C++ 排序函数 sort(),qsort()的含义与用法 ,字符串string 的逆序排序等

    上学时我们很多学了很多种排序算法,不过在c++stl中也封装了sort等函数,头文件是#include <algorithm> 函数名 功能描述 sort 对给定区间所有元素进行排序 st ...

  3. qsort的几种用法

    #include<stdio.h> #include<stdlib.h> int cmp(const void *a,const void *b){ return *(int ...

  4. C/C++中qsort()以及sort()的用法

    最近学弟们问快速排序的比较多,今天自己就做一下总结,快速排序在库函数里面有现成的,不用自己实现,调用一下就可以达到自己想要的结果,掌握以后就可以完全摒弃冒泡和选择了,并且时间复杂度也从O(n*n)提升 ...

  5. c语言中使用自带的qsort(结构体排序)+ 快排

    c中没有自带的sort函数emm 不过有自带的qsort函数 (其实用法都差不多(只是我经常以为c中有sort 头文件要用 #include <stdlib.h> 一定要重新把指针指向的值 ...

  6. qsort(),sort()排序函数

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

  7. C++中sort()及qsort() (不完整介绍)

    在平时刷算法题和oj的时候,排序算法是最经常用到的算法之一:且在各类算法书的目录中 也通常是将各种排序算法放在最前面来讲,可见排序算法的重要性.可能许多人都在算法书中有学过冒泡.快速排序的方法,也都大 ...

  8. C++知识点总结(6)

    1.double和float的存储方式 float遵从的是IEEE R32.24 ,而double 遵从的是R64.53.无论是单精度还是双精度在存储中都分为三个部分: 符号位(Sign) : 0代表 ...

  9. C语言中qsort函数用法

    C语言中qsort函数用法-示例分析    本文实例汇总介绍了C语言中qsort函数用法,包括针对各种数据类型参数的排序,非常具有实用价值非常具有实用价值. 分享给大家供大家参考.C语言中的qsort ...

随机推荐

  1. GlusterFS分布式存储集群部署记录-相关补充

    接着上一篇Centos7下GlusterFS分布式存储集群环境部署记录文档,继续做一些补充记录,希望能加深对GlusterFS存储操作的理解和熟悉度. ======================== ...

  2. Puppet常识梳理

    Puppet简单介绍 1)puppet是一种Linux/Unix平台下的集中配置管理系统,使用自有的puppet描述语言,可管理配置文件.用户.cron任务.软件包.系统服务等.puppet把这些系统 ...

  3. CF 1047 C. Enlarge GCD

    传送门 [http://codeforces.com/contest/1047/problem/C] 题意 给你n个数,移除最少的数字使剩下的数字GCD大于初始GCD 思路 需要一点暴力的技巧,先求出 ...

  4. 《Linux内核分析》第八周学习总结

    <Linux内核分析>第八周学习总结                                      ——进程的切换和系统的一般执行过程 姓名:王玮怡  学号:20135116 ...

  5. 北京大学信息科学技术学院本科生课程体系课程大纲选登——计算机网络与WEB技术

  6. 『编程题全队』Beata阶段项目复审

    小组的名字和链接 优点 缺点,bug 报告(部分包括建议) 最终名次 想不出队名 1. 界面简洁大方2. 有搜索功能 1. 已经完成的活动缺了点提示界面2. 似乎界面有一点点卡顿目标实现:基本实现找到 ...

  7. jquery 加載

    load(url,data,callback)從遠程服務器加載數據,并放入到被選中元素上: url是必須有的,希望加載的url: data是可選的,表示和請求一起發送的鍵值對數據: callback是 ...

  8. html 佈局

    html常見佈局方式有以下幾種: 1.使用div的html 利用div可以為html實現多列佈局. 2.使用html5的網站佈局, 利用html新增的header.footer.nav.section ...

  9. 如何取消浏览器护眼色 Lodop打印图片有窗口颜色的边框

    Lodop打印图片出现了边框,然而通常情况下是没有边框的,由于Lodop是基于本机的ie进行解析的,和IE的设置有关.用户的电脑和习惯千差万别,有人喜欢给浏览器加上护眼色,而这一个行为可能导致Lodo ...

  10. Django-website 程序案例系列-4 ORM数据库操作

    数据库表的创建: 使用mysql时注意,在setting.py中的设置: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql' ...