Cod sursa(job #757981)

Utilizator angelicheartMicu Ana angelicheart Data 14 iunie 2012 00:11:52
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <fstream>
#include <cstdlib>
using namespace std;

const int N = 500005;
int v[N], aux[N], n;

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

inline void sch(int& a, int& b)
{
    int c = a;
    a = b;
    b = c;
}

void bubble()
{
    bool okay = true;

    while (okay)
    {
        okay = false;

        for (int i = 1 ; i < n ; i++)
            if (v[i] > v[i + 1])
            {
                okay = true;
                sch(v[i], v[i + 1]);
            }
    }
}

int main()
{
    in >> n;

    for (int i = 1 ; i <= n ; i++)
        in >> v[i];

    bubble();

    for (int i = 1 ; i <= n ; i++)
        out << v[i] << " ";
    out << "\n";
    return 0;
}