Sorting a Vector

Lab C++ Beginner 10 points

Read n integers and print them sorted ascending and then descending. Note: in this lab's compiler, always pass a comparator to sort for numbers: less<int>() or greater<int>().

Input

Line 1: n. Line 2: n integers.

Output

Two lines.

Example

Input

6
5 2 9 1 5 6

Output

1 2 5 5 6 9
9 6 5 5 2 1
Sample input available Sample output available
Editor
C++

Hints

#include <algorithm> and <functional>.

sort(begin, end, greater<int>()) sorts descending.

Solution explanation

sort runs in O(n log n). The comparator decides the order.

Show solution
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

void print(const vector<int> &v) {
    for (size_t i = 0; i < v.size(); i++) cout << v[i] << (i + 1 < v.size() ? " " : "\n");
}

int main() {
    int n;
    cin >> n;
    vector<int> v(n);
    for (int &x : v) cin >> x;
    sort(v.begin(), v.end(), less<int>());
    print(v);
    sort(v.begin(), v.end(), greater<int>());
    print(v);
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD