1020: 排序问题<2>

Description

将十个数进行从大到小的顺序进行排列

Input

十个整数

Output

以从大到小的顺序输出这个十个数,每一个数末尾有一个空格~

Sample Input

1 2 3 4 5 6 7 8 9 10

Sample Output

10 9 8 7 6 5 4 3 2 1

#include <stdio.h>
#include <algorithm>
using namespace std;
int comp(int i,int j){
    return i>j;
}
int main() {
    int a[10];
    int i;
    for (i = 0; i < 10; i++) {
        scanf("%d", &a[i]);
    }
    sort(a, a + 10,comp);
    for (i = 0; i < 10; i++) {
        printf("%d ", a[i]);
    }
    return 0;
}

此程序里面调用了库函数,其中的sort排序复杂性平均而言,在距离介于第一和最活的,刚过去的*大约执行N*log2(N)(在哪里N就是这个距离)元素的比较,最多是那么多的元素交换(或移动)。

Sort函数包含在头文件为#include<algorithm>的c++标准库中。Sort函数使用模板: Sort(start,end,排序方法)开始为数组的起始地址。

Sort函数有三个参数:

(1)第一个是要排序的数组的起始地址。

(2)第二个是结束的地址(最后一位要排序的地址的下一地址)

(3)第三个参数是排序的方法,可以是从大到小也可是从小到大,还可以不写第三个参数,此时默认的排序方法是从小到大排序。

猜你喜欢

转载自blog.csdn.net/meng1ge/article/details/81134451