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;
}



目录
相关文章
|
21小时前
|
搜索推荐 Python
冒泡法详解
冒泡法详解
|
29天前
qsort排序的基本用法
qsort排序的基本用法
14 0
|
1月前
|
C#
C#中sort排序相关用法介绍
C#中sort排序相关用法介绍
|
1月前
字符串排序
字符串排序。
22 1
|
1月前
|
C++
C++中sort排序
C++中sort排序
|
1月前
|
JavaScript 前端开发
sort函数排序
sort函数排序
32 0
sort函数排序
|
1月前
|
人工智能
数组排序,查找
数组排序,查找
|
11月前
了解冒泡排序,并写出一个函数进行排序,拍成升序
了解冒泡排序,并写出一个函数进行排序,拍成升序
|
算法 搜索推荐 编译器
排序函数qsort和sort那点事
排序函数qsort和sort那点事
117 0
C/C++编程题之字符串排序
C/C++编程题之字符串排序