浙大版《数据结构(第2版)》题目集-练习7.1

练习7.1 排序 (25point(s))

给定N个(长整型范围内的)整数,要求输出从小到大排序后的结果。

本题旨在测试各种不同的排序算法在各种数据情况下的表现。各组测试数据特点如下:

  • 数据1:只有1个元素;
  • 数据2:11个不相同的整数,测试基本正确性;
  • 数据3:103个随机整数;
  • 数据4:104个随机整数;
  • 数据5:105个随机整数;
  • 数据6:105个顺序整数;
  • 数据7:105个逆序整数;
  • 数据8:105个基本有序的整数;
  • 数据9:105个随机正整数,每个数字不超过1000。

Example:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int N;
    cin >> N;
    vector<long> List;
    List.reserve(N);
    while(N--) {
        int k;
        cin >> k;
        List.push_back(k); 
    }
    sort(List.begin(), List.end());
    bool space = false;
    for(auto &x : List) {
        if(!space) space = true;
        else cout << ' ';
        cout << x;
    }
    return 0;
}

思路:

STL 的 Vector + sort

猜你喜欢

转载自blog.csdn.net/u012571715/article/details/113364135
今日推荐