Skip to content

11024: 【原1024】排序

题目

题目描述

author: 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/1024

Input Format

第一行:正整数\(n\),表示需要排序的数字的个数;

第二行:\(n\)个正整数,代表需要排序的数字。

Output Format

输出共一行,\(n\)个正整数,为从小到大排序后的结果。

Sample Input

3
2 1 3

Sample Output

1 2 3

Limits

\( n < 10000 \),时间限制1000ms,内存限制30000kb。

FineArtz's solution

/* 排序 */
#include <iostream>
#include <algorithm>
using namespace std;

int n;
int a[10005];

void qsort(int l, int h){
    int i = l, j = h;
    int m = a[(l + j) / 2];
    while (i < j){
        while (a[i] < m) ++i;
        while (a[j] > m) --j;
        if (i <= j){
            int t = a[i];
            a[i] = a[j];
            a[j] = t;
            ++i;
            --j;
        }
    }
    if (i < h) qsort(i, h);
    if (j > l) qsort(l, j);
}

int main(){
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    qsort(1, n);
    for (int i = 1; i <= n; ++i)
        cout << a[i] << ' ';
    cout << endl;
    return 0;
}

ligongzzz's solution

#include "iostream"
#include "vector"
#include "algorithm"
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    vector<int> vector_data;

    int n;
    cin >> n;

    for (int i = 0; i < n; ++i) {
        int temp;
        cin >> temp;
        vector_data.push_back(temp);
    }

    sort(vector_data.begin(), vector_data.end());

    for (auto p : vector_data)
        cout << p << " ";

    return 0;
}

yyong119's solution

#include <iostream>
#include <algorithm>
int n,i;
int a[10000];
int main(){
    using namespace std;
    cin>>n;
    for (i=0; i<=n-1; i++) cin>>a[i];
    sort(a,a+n);
    for (i=0; i<=n-2; i++) cout<<a[i]<<" ";
    cout<<a[n-1];
    return 0;
}