Cod sursa(job #3003645)

Utilizator AndreiKatsukiAndrei Dogarel AndreiKatsuki Data 15 martie 2023 20:38:43
Problema Sortare prin comparare Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("algsort.in");
ofstream g("algsort.out");

const int NMAX = 5e5 + 5;
int a[NMAX], n;

int main(){
    f >> n;
    for(int i=1;i<=n;++i){
        f >> a[i];
    }
    for(int k = 0; k < n; ++k){
        bool ok = false;
        for(int i = 2; i <= n - k; ++i){
            if(a[i] < a[i-1]){
                swap(a[i], a[i-1]);
                ok = true;
            }
        }
        if(!ok){
            break;
        }
        else{
            ok = false;
        }
    }
    for(int i=1;i<=n;++i){
        g << a[i] << " ";
    }
    return 0;
}