sort排序

简介: sort排序
/* 对int 型排序 */ 
#include<stdio.h>
#include<algorithm>//sort头文件 
using namespace std;// c++必写 
/*bool cmp(int a,int b)
{
    return a>b;
} */ // 去掉注释可以从大到小排序  sort 默认是从小到大排
int main()
{
    int n,a[100];
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    //sort(a,a+n,cmp); //去掉注释可以从大到小排序  sort 默认是从小到大排
    for(int i=0;i<n-1;i++)
        printf("%d ",a[i]);
    printf("%d\n",a[n-1]);
    return 0;
}


/*   对 char 型进行排序 */
#include<stdio.h>
#include<algorithm>//sort头文件 
#include<string.h> 
using namespace std;// c++必写 
bool cmp(char a,char b)
{
    return a>b;
} 
int main()
{
    int n;
    char a[100];
        scanf("%s",a);
        int len=strlen(a);
       sort(a,a+len);
     puts(a);
    return 0;
 } 


 /*  对字符串排序*/
#include<stdio.h>
#include<algorithm>//sort头文件 
#include<string.h> 
#include<iostream>
#include<string> // string 头文件 
using namespace std;// c++必写
int main()
{
    string a[1000];
    int i,n;
    scanf("%d",&n);
    for(i=0;i<n;i++)
       cin>>a[i];
    sort(a,a+n);
    for(i=0;i<n;i++)
       cout<<a[i]<<endl;
    return 0;
}



目录
相关文章
|
5月前
qsort函数专题
qsort函数专题
33 2
|
5月前
|
搜索推荐 算法 C语言
冒泡排序:从小到大轻松搞定数组排序(c语言代码)
冒泡排序:从小到大轻松搞定数组排序(c语言代码)
181 0
|
3月前
字符串排序
【7月更文挑战第6天】字符串排序。
20 4
|
4月前
|
C语言
qsort函数的应用
qsort函数的应用
23 0
|
5月前
qsort排序的基本用法
qsort排序的基本用法
22 0
|
5月前
|
C#
C#中sort排序相关用法介绍
C#中sort排序相关用法介绍
|
5月前
|
搜索推荐
【qsort函数实现】
【qsort函数实现】
|
5月前
|
C++
C++中sort排序
C++中sort排序
|
5月前
|
JavaScript 前端开发
sort函数排序
sort函数排序
46 0
sort函数排序
|
10月前
|
容器
sort函数
sort函数