Zhejiang University Edition "Data Structure (Second Edition)" Question Collection-Exercise 7.1

Exercise 7.1 Sorting (25point(s))

Given N integers (in the range of long integers), it is required to output the results sorted from small to large.

This question aims to test the performance of various sorting algorithms in various data situations. The characteristics of each group of test data are as follows:

  • Data 1: Only 1 element;
  • Data 2: 11 different integers, the test is basically correct;
  • Data 3: 103 random integers;
  • Data 4: 104 random integers;
  • Data 5: 105 random integers;
  • Data 6: 105 sequential integers;
  • Data 7: 105 reverse-order integers;
  • Data 8: 105 basically ordered integers;
  • Data 9: 105 random positive integers, each number does not exceed 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;
}

Ideas:

STL's Vector + sort

Guess you like

Origin blog.csdn.net/u012571715/article/details/113364135