Cod sursa(job #1624012)

Utilizator tudormaximTudor Maxim tudormaxim Data 1 martie 2016 23:28:28
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

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

const int nmax = 500005;
int v[nmax], poz=nmax-1;
char buffer[nmax];

void read(int &x)
{
    while(!(buffer[poz]>='0' && buffer[poz]<='9'))
    {
        poz++;
        if(poz == nmax)
        {
            fin.read(buffer, nmax);
            poz = 0;
        }
    }
    x = 0;
    while(buffer[poz]>='0' && buffer[poz]<='9')
    {
        x = x*10 + (buffer[poz] - '0');
        poz++;
        if(poz == nmax)
        {
            fin.read(buffer, nmax);
            poz = 0;
        }
    }
}

void selection_sort(int st, int dr)
{
    int i, j, min_poz;
    for(i=st; i<dr; i++)
    {
        min_poz=i;
        for(j=i+1; j<=dr; j++)
            if(v[j] < v[min_poz]) min_poz=j;
        if(v[min_poz]!=v[i]) swap(v[min_poz], v[i]);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    int n, i;
    read(n);
    for(i=1; i<=n; i++)
        read(v[i]);
    selection_sort(1, n);
    for(i=1; i<=n; i++)
        fout << v[i] << " ";
    fin.close();
    fout.close();
    return 0;
}