Qsort(c)_Sort(c++)用法
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++)用法的更多相关文章
- qsort()与sort的用法(收藏)
sort()函数是C++中的排序函数其头文件为:#include<algorithm>头文件: qsort()是C中的排序函数,其头文件为:#include<stdlib.h> ...
- C++ 排序函数 sort(),qsort()的含义与用法 ,字符串string 的逆序排序等
上学时我们很多学了很多种排序算法,不过在c++stl中也封装了sort等函数,头文件是#include <algorithm> 函数名 功能描述 sort 对给定区间所有元素进行排序 st ...
- qsort的几种用法
#include<stdio.h> #include<stdlib.h> int cmp(const void *a,const void *b){ return *(int ...
- C/C++中qsort()以及sort()的用法
最近学弟们问快速排序的比较多,今天自己就做一下总结,快速排序在库函数里面有现成的,不用自己实现,调用一下就可以达到自己想要的结果,掌握以后就可以完全摒弃冒泡和选择了,并且时间复杂度也从O(n*n)提升 ...
- c语言中使用自带的qsort(结构体排序)+ 快排
c中没有自带的sort函数emm 不过有自带的qsort函数 (其实用法都差不多(只是我经常以为c中有sort 头文件要用 #include <stdlib.h> 一定要重新把指针指向的值 ...
- qsort(),sort()排序函数
一.qsort()函数 功 能: 使用快速排序例程进行排序 头文件:stdlib.h 用 法: void qsort(void *base,int nelem,int width,int (*fcmp ...
- C++中sort()及qsort() (不完整介绍)
在平时刷算法题和oj的时候,排序算法是最经常用到的算法之一:且在各类算法书的目录中 也通常是将各种排序算法放在最前面来讲,可见排序算法的重要性.可能许多人都在算法书中有学过冒泡.快速排序的方法,也都大 ...
- C++知识点总结(6)
1.double和float的存储方式 float遵从的是IEEE R32.24 ,而double 遵从的是R64.53.无论是单精度还是双精度在存储中都分为三个部分: 符号位(Sign) : 0代表 ...
- C语言中qsort函数用法
C语言中qsort函数用法-示例分析 本文实例汇总介绍了C语言中qsort函数用法,包括针对各种数据类型参数的排序,非常具有实用价值非常具有实用价值. 分享给大家供大家参考.C语言中的qsort ...
随机推荐
- Runaway argument错误 [Overleaf: 在线Latex] [Type 3问题后续]
[背景与问题描述] 在Latex中,经常出现各种问题: Runaway argument? {\contentsline {subsection}{\numberline {6.3}General c ...
- 基于Nginx+Keepalived的LB服务监控(邮件报警)
IDC两台机器上部署了Nginx+Keepalived主从模式的LB代理负载层,现在需要对LB进行每日巡检和服务监控,利用SendEmail邮件监控. 0)SendEmail部署 参考:http:// ...
- Linux运维笔记-日常操作命令总结(1)
在linux日常运维中,我们平时会用到很多常规的操作命令. 查看服务器的外网ip [root@redis-new01 ~]# curl ifconfig.me [root@redis-new01 ~] ...
- kvm虚拟化管理平台WebVirtMgr部署-完整记录(1)
公司机房有一台2U的服务器(64G内存,32核),由于近期新增业务比较多,测试机也要新增,服务器资源十分有限.所以打算在这台2U服务器上部署kvm虚拟化,虚出多台VM出来,以应对新的测试需求.当KVM ...
- WinForm多线程+委托防止界面假死
当有大量数据需要计算.显示在界面或者调用sleep函数时,容易导致界面卡死,可以采用多线程加委托的方法解决 using System; using System.Collections.Generic ...
- apacheTomcat
Window+R ------>cmd || Window PowerShell apacheTomcat\bin> ./startup.sh
- PHP输入流 php://input 相关【转】
为什么xml_rpc服务端读取数据都是通过file_get_contents(‘php://input', ‘r').而不是从$_POST中读取,正是因为xml_rpc数据规格是xml,它的Conte ...
- html 空白汉字占位符 
在爬取京东评论时,复制html内容,发现文本中有些空格的宽度没见过.后来用htmlParser解析html页面时,发现这些空格都被替换为 . 12288是Unicode编码,&#表示宋体,&a ...
- Spring Cloud Zipkin
Zipkin the idea is from the googlge paper:Dapper https://yq.aliyun.com/articles/60165 https://www.e4 ...
- CentOS下使用VirtualBox 安装 Windows虚拟机的简单方法
1.物理服务器安装CentOS7.5 2. 安装VNC 3. 关闭防火墙,关闭selinux,上传virtualbox的rpm包. http://download.virtualbox.org/vir ...